Bash syntax condensed

Single line scripts

for var in item1 item2 item3; do echo $var; done

Arithmetic

echo 1 + 1 will print 1 + 1. To (integer) evaluate use echo $((1 + 1)) or echo $[1 + 1].

For arbitrary precision pipe expression to bc -l, i.e.: echo 3/4 | bc -l.

Command line arguments

  • $0 is the name of the script.
  • $n is the nth parameter.
  • $# is the number of parameters (excluding $0)
  • $@ is all parameters (remaining: see below).

Useful motif to have an “all remaining parameters” parameter (note that all single parameters take $1):

var1=$1
shift
var2=$1
shift
alltherest=$@

if

if [ $# -eq 0 ]
then
   echo $usage
   exit
fi

loops

for var in item1 item2 item3
do
   echo $var
done

Input

read varname
echo "varname: $varname"
bash_syntax_condensed.txt · Last modified: 2009/01/16 10:15 by jobriath
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki