Working with the set command (with examples)
Display the names and values of shell variables
To display the names and values of shell variables, you can simply use the set
command without any arguments.
set
Motivation: This is useful when you want to see all the current shell variables and their values. It can help you understand the current environment and troubleshoot any issues related to variables.
Example Output:
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
...
Mark variables that are modified or created for export
To mark variables that are modified or created for export, use the -a
option with the set
command.
set -a
Motivation: This is useful when you want to ensure that any variables you modify or create will be exported to the environment of subsequently executed commands. It is commonly used when writing scripts that need to pass variables to other programs.
Example Output: N/A
Notify of job termination immediately
To notify of job termination immediately, use the -b
option with the set
command.
set -b
Motivation: This option is used to enable job control in the shell. When enabled, the shell will notify the user immediately whenever a background job terminates. This can be helpful in monitoring and managing background tasks.
Example Output: N/A
Set various options, e.g. enable vi style line editing
To set various options, such as enabling vi style line editing, use the -o
option followed by the desired option. In this example, we enable vi style line editing by setting the vi
option.
set -o vi
Motivation: This option enables command line editing using vi key bindings. If you are familiar with vi or prefer its command syntax, this can significantly improve your efficiency when editing command lines in the shell.
Example Output: N/A
Set the shell to exit as soon as the first error is encountered
To set the shell to exit as soon as the first error is encountered, use the -e
option with the set
command.
set -e
Motivation: This option is commonly used in shell scripts to ensure that the script stops execution immediately when an error occurs. It helps in catching and handling errors early, preventing further potential issues.
Example Output: N/A