Array variables are created automatically when they are accessed
[me@linuxbox ~]$ a[1]=foo [me@linuxbox ~]$ echo ${a[1]} foo Arrays can also be created with the declare command. declare -a a
Arrays can also be created at the same time as values are values are added days=(Sun Mon Tue Wed Thu Fri Sat) Note: can specify indexes days=([0]=Sun [1]=Mon [2]=Tue [3]=Wed [4]=Thu [5]=Fri [6]=Sat)
If testing reveals a problem the next step is debugging.
Isolate
Isolate the problem area by commenting out sections of code
if [[ -d $dir_name ]]; then if cd $dir_name; then rm * else echo "cannot cd to '$dir_name'" >&2 exit 1 fi # else # echo "no such directory: '$dir_name'" >&2 # exit 1 fi Tracing
Tracing by placing informative messages in the script
echo "preparing to delete files" >&2 if [[ -d $dir_name ]]; then if cd $dir_name; then echo "deleting files" >&2 rm * else echo "cannot cd to '$dir_name'" >&2 exit 1 fi else echo "no such directory: '$dir_name'" >&2 exit 1 fi echo "file deletion complete" >&2 Send messages to standard error to separate them from normal output.
...
dirs # Prints current directory stack.
popd # Removes current directory from stack and cds into previous directory on the stack.
pushd # Usage: pushd /some/dir
Pushes directory onto stack and cds into that directory.
Example # i[akraker@localhost ~]$ pushd ~/wiki ~/wiki ~ i[akraker@localhost wiki]$ pushd ~/dotfiles ~/dotfiles ~/wiki ~ i[akraker@localhost dotfiles]$ dirs ~/dotfiles ~/wiki ~ i[akraker@localhost dotfiles]$ popd ~/wiki ~ i[akraker@localhost wiki]$ popd ~
0 = success 1-255 = failure echo $? to get exit status of last run command
[[ expression ]] [[ string1 =~ regex ]] supports regex [[ string1 == pattern* ]] supports shell globbing/pattern matching
(( expression )) Designed for integers Supports full set of arithmetic evaluations
Expression Is True If file1 -ef file2 file1 and file2 the two filenames refer to the same hardlinked file file1 -nt file2 file1 is newer than file2 file1 -ot file2 file1 is older than file2 -b file file exists and is a block-special (device) file -c file file exists and is a character-special (device) file -d file file exists and is a directory -e file file exists -f file file exists and is a regular file -g file file exists and is set-group-ID -G file file exists and is owned by effective group ID -k file file exists and has its “stkicky bit” set -L file file exists and is a symbolic link -O file file exists and is owned by the effective user ID -p file file exists and is a named pipe -r file file exists and is readable -s file file exists and has a length greater than zero -S file file exists and is a network socket -t fd fd is a file descriptor directed to/from the terminal.
...
Introduced in recent versions of bash for (( expression1; expression2; expression3 )); do dommands done
Here expression1, expression2, and expression3 are arithmetic and commands are the commands to be performed.
#!/bin/bash # simple_counter: demo of C style for command for (( i=0; i<5; i=i+1 )); do echo $i done
for variable [in words]; do commands done
[me@linuxbox ~]$ for i in {A..D}; do echo $i; done A B C D #!/bin/bash # longest-word: find longest string in a file while [[ -n "$1" ]]; do if [[ -r "$1" ]]; then max_word= max_len=0 for i in $(strings "$1"); do len="$(echo -n "$i" | wc -c)" if (( len > max_len )); then max_len="$len" max_word="$i" fi done echo "$1: '$max_word' ($max_lencharacters)" fi shift done If the optional in words portion of the for loop is omitted, for defaults to processing the positional parameters.
...
Can be created two ways
funtion name { commands return } # Simpler and generally preferred form name () { commands return } Make excellent replacements for aliases
Embeds a body of text in a script.
command << token text token
cat << _EOF_ <html> <head> <title>$TITLE</title> </head> <body> <h1>$TITLE</h1> <p>$TIMESTAMP</p> </body> </html> _EOF_