The set Command
- “set” w/o arguments displays all variables and functions.
- “set” followed by arguments will reset the current positional parameters.
- For example: set Athos Porthos Aramis
- Results: $1=Athos $2=Porthos $3=Aramis
Turning on and off trace mode
- set -x turns on trace mode
- set +x turns off trace mode
- var1=abc; set -x
- if [ "$var1" = "abc" ]; then echo a; date; echo $var; fi
- The output will be:
- + '[' abc = abc ']'
- + echo a
- a
- + date
- Monday, June 30, 2014 12:00:57 AM EDT
- + echo file.cpp
- file.cpp
The -- option
- The - - option can be used with set to:
- Keep set from displaying all variables if no arguments are given.
- Keep set from interpreting a argument that starts with a hyphen(-) as an option.
The $0 Variable
- The $0 variable holds the name of the command beginning executed.
- From the command line, echo $0 will return “-bash” if running the bash shell.From a script, it returns the name of the script.
The $IFS Variable
The Internal Field Separator (IFS) is used to parse input.
- echo “$IFS”will return blank lines
- echo “$IFS” | od -b will return something like
- 0000000 040 011 012 012
- 0000004
- This translates to space, tab, and newline.
- The IFS can be changed to interpret data correcting when other field separators are used.
The readonly & unset Commands
- The readonly command sets variables to read only status. Afterwards they can not be unset or changed.
- The unset command removes variables from the variable table.