!!!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.

You must logout and back in in order for changes to .profile to take effect.

------

!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

"$@"    passing all arguments to another shell or program
        Use with double quotes.  If you do not do it this way,
        arguments with spaces will not be handled correctly.
}}}
--------------------------------------------------
!Quotes

$ variables do not get evaluated within single quotes.

$ variables get evaluated within double quotes.

--------------------------------------------------
!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