!!!The bash shell

These are programming notes for programming in the bash shell.

------

!bash startup

.bashrc is executed whenever a new bash session is started (such as opening a new terminal) 
and .profile is executed when a user logs in. 
Programs such as Netbeans are not able to see environment variables set in .bashrc as it is run after the environment Netbeans is started in has been constructed.  Environment variables set in .profile can be seen by programs started after login.

------

!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
   ...
fi
if [ cond ] ||  [ cond ]; then
   ...
fi
}}}
------------------------------------

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