!!!The bash shell

These are programming notes for programming in the bash shell.

------

!Redirecting IO
{{{
2>file		send stderr to file

2>&1		send stderr to stdout
}}}
--------------------------------------------------
!Accessing shell arguments
{{{
$0	command name

$1	first argument

$@	all arguments

$#	number of arguments

shift	shift arguments
}}}
--------------------------------------------------
!List iteration
(In this example, through all the arguments)
{{{
for var in $@; do
    echo $var
done
}}}
--------------------------------------------------
!Variable assignment
{{{
var=val      no space around =
}}}
--------------------------------------------------
!for loops
{{{
for ((i=1 ; i < 5 ; i++)); do
    echo $i
done
}}}
--------------------------------------------------

!Arrays

Start from 0
{{{
a[3]=val	assignment

${a[3]}		get value

a=(val1 val2 val3)	make array

${#a[@]}	number of elements

${a[@]}		all elements as a list
}}}
--------------------------------------------------
!Conditionals
{{{
if [ condition ]; then
   ....
elif [ condition ]; then
   ....
else
   ....
fi
}}}
--------------------------------------------------

!Conditions

{{{
-f file		if file exists
-d dir		if dir exists
a = b		string equal
a != b		string not equal
-z x		string is null
x		string is not null
x -eq y		x numerically equal to y
x -ne y		x numerically not equal to y
x -gt y		x gretar than y
x -lt y		x less than y
x -ge y		x gretar than or equal to y
x -le y		x less than or equal to y
! cond		not condition

if [cond] &&  [cond]; then
   ...
}}}
------------------------------------

!here documents - inline input
{{{
cmd << label
..
..
..
label
}}}
all lines up to label are input into command