Bash Quick Reference

A quick reference to Bash scripting. This isn't intended to be a tutorial, just a set of quick notes to remind me of Bash scripting syntax etc. If you want to learn Bash scripting, try instead. FIMXE - add a lot more :) ===== if/else statements ===== Simple test of equality: <code bash> if [ “$animal” == “badger” ]; then echo “Want a mushroom?” fi if [ ! “$force” ] && [ -e “$destfile” ]; then echo “$destfile already exists, skipping” fi if [ `id -u` != “0” ]; then echo “Sorry, you are not root.” exit 1 fi </code> (Always quote var names, otherwise you'll get errors if the var is empty). File tests: <code bash> if [ -e /etc/passwd ]; then echo “Yes, we have a passwd file”; fi if [ ! -d /tmp ]; then echo “Where's our /tmp dir gone?” fi </code> ===== For loops ===== <code bash> for 1); do echo $i done </code> Looping through a set of values: <code bash> for $animal in badger snake raccoon; do echo “Mmm, deep fry me a $animal” done </code> Using seq to loop through a sequence: <code bash> for $i in `seq 1 10`; do echo $i done </code> continue breaks out of a loop: <code bash> for my FILE in /etc/*; do if "$FILE" =~ "pass"; then echo “This is some kind of password file, skipping”; continue; fi # do something with $FILE done </code> ===== while loops ===== <code bash> while read input; do echo “you said: $input” done </code> ===== regular expressions ===== Regular expressions worked for me under Bash 3.00: <code bash> `echo bash –version` for animal in badger snake camel monkey terrier; do if terrier)" ; then echo “ooh it's a scary $animal”; fi done </code> On a machine where it works, that script outputs: <code bash> [dave@otherbox:~]$ /tmp/ GNU bash, version 3.00.16(2)-release (i486-slackware-linux-gnu) Copyright (C) 2004 Free Software Foundation, Inc. Uh-oh it's a scary snake Uh-oh it's a scary terrier </code> But on another machine, the same script outputs: <code bash> [davidp@firstbox:~]$ /tmp/ GNU bash, version 3.2.5(1)-release (i686-pc-linux-gnu) Copyright (C) 2005 Free Software Foundation, Inc. </code> A later version of Bash, and it doesn't work. Caveat scriptor. ===== debugging ===== To see the source as it executes through it, run your script like this: <code> bash -v </code> or add this to the beginning of your script: <code> set -v </code> To see the expressions evaluated as the script runs, execute your script like this: <code> bash -x </code> or add this to the beginning of your script: <code> set -x </code> ===== special shell variables ===== ^ Variable ^ Meaning ^ | $0 | Name of script | | $1 | Positional parameter #1 | | $2 - $9 | Positional parameters #2 - #9 | | ${10} | Positional parameter #10 | | $# | Number of positional parameters | | “$*” | All the positional parameters (as a single word) * | | “$@” | All the positional parameters (as separate strings) | | ${#*} | Number of command line parameters passed to script | | ${#@} | Number of command line parameters passed to script | | $? | Return value | | $$ | Process ID (PID) of script | | $- | Flags passed to script (using set) | | $_ | Last argument of previous command | | $! | Process ID (PID) of last job run in background | * Must be quoted, otherwise it defaults to “$@”. For plenty of quick reference tables see

1) i=1;i⇐10;i+=1
bash/quickref.txt · Last modified: 2010/02/26 10:45 (external edit)
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki