100 Useful Command-Line Utilities

by Oliver; 2014

48. set, unset

Use set to set various properties of your shell, somewhat analogous to a Preferences section in a GUI.

E.g., use vi style key bindings in the terminal:
$ set -o vi
Use emacs style key bindings in the terminal:
$ set -o emacs
You can use set with the -x flag to debug:
set -x  # activate debugging from here
.
.
.
set +x  # de-activate debugging from here
This causes all commands to be echoed to std:err before they are run. For example, consider the following script:
#!/bin/bash

set -eux

# the other flags are:
# -e Exit immediately if a simple command exits with a non-zero status
# -u Treat unset variables as an error when performing parameter expansion

echo hello
sleep 5
echo joe
This will echo every command before running it. The output is:
+ echo hello
hello
+ sleep 5
+ echo joe
joe
Changing gears, you can use unset to clear variables, as in:
$ TEST=asdf
$ echo $TEST
asdf
$ unset TEST
$ echo $TEST

<PREV   NEXT>