catalogue
1. Execute the set command without options
Introduction to set command
The set command is a command that beginners in the shell seldom touch, but it is very useful (the shell we say here refers to bash). The set command is a built-in command of the shell interpreter. It is used to set the properties of the shell interpreter, so as to control some behaviors of the shell interpreter.
~/programming/test $ type set set is a shell builtin
Using the set command
1. Execute the set command without options
When the set command is executed without options, all variables of the current shell will be output. The output format is the same as the variable assignment format in the shell script: name=value. Therefore, the output of the set command can be directly used as the input of an stdin.
2. Basic grammar
The basic syntax of the set command is as follows (from bash's man manual):
set [--abefhkmnptuvxBCEHPT] [-o option-name] [arg ...] set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]
set switches different features of the shell through options, and each feature corresponds to an option. Each feature can be configured in two ways:
-
One is to specify options directly in the form of set -e and set +e.
-
The other is through the form of set -o errexit and set +o errexit, that is, the option name is specified through the option o.
I think you must be very curious about whether to use the + or - sign. In the set command, the option is preceded by a - sign to turn on the option and + to turn off the option.
3. Introduction to options
-o
Executing set -o will output the current set option configuration:
~/programming/test$ set -o allexport off braceexpand on emacs on errexit off errtrace off functrace off hashall on histexpand on history on ignoreeof off interactive-comments on keyword off monitor on noclobber off noexec off noglob off nolog off notify off nounset off onecmd off physical off pipefail off posix off privileged off verbose off vi off xtrace off
+o
Executing set +o also outputs the configuration of the current set option, but the output form is a series of set commands. This output form is generally used to reconstruct the current set configuration item.
~/programming/test$ set +o set +o allexport set -o braceexpand set -o emacs set +o errexit set +o errtrace set +o functrace set -o hashall set -o histexpand set -o history set +o ignoreeof set -o interactive-comments set +o keyword set -o monitor set +o noclobber set +o noexec set +o noglob set +o nolog set +o notify set +o nounset set +o onecmd set +o physical set +o pipefail set +o posix set +o privileged set +o verbose set +o vi set +o xtrace
-e or -o errexit
When this option is set, the shell will exit immediately when a command fails.
-n or -o noexec
When this option is set, the shell reads commands but does not execute them. This option can be used to check the shell script for syntax errors.
-u or -o unset
With this option set, when the shell wants to extend a variable that has not been set, the shell must output information to stderr and exit immediately. But the interactive shell should not exit.
-x or -o xtrace
After this option is set, for each command to be executed, the shell outputs trace to stderr after extending the command (parameter extension) and before executing the command.
-o pipefail
This option affects the return value of the pipeline. By default, the return value of a pipeline is the return value of the last command. For example, the return value of the pipeline cmda | cmdb | cmdc is determined by the return value of the cmdc command. If the pipefail option is specified, the return value of the pipeline will be determined by the last failed command, which means that if a command fails, a non-zero value will be returned. If all commands are successful, success is returned.
example
#!/bin/bash set -o xtrace set -o errexit # You can comment this out to see how the execution effect is different. echo "Before" ls filenoexists # ls does not exist echo "After"