This is version . It is not the current version, and thus it cannot be edited.
Back to current version   Restore this version

The bash shell#

These are programming notes for programming in the bash shell.


bash startup#

~/.bashrcexecuted when a new shell is started
~/.profileexecuted when the user logs in
/etc/profile.d/*executed when all users login

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 or /etc/profile.d/*to take affect.


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

Add new attachment

Only authorized users are allowed to upload new attachments.
« This particular version was published on 06-Mar-2015 08:56 by BlakeMcBride.