Set command

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search

Use the set command to set or unset values of shell options and positional parameters. You can change the value of shell attributes and positional parameters, or display the names and values of shell variables using set command.

Syntax

The basic syntax:

set [options]
set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]

set command examples

Let us see some common examples of set command.

Turn debugging information on or off

Pass the -x option to show commands and their arguments as they are executed. Useful for debugging shell script:

#!/bin/bash
set -x # Turn on debug
....
...
...
set +x # Turn off debug
....
...

Understanding shell variables

Quoting from the bash man page:

Shell variable Description
allexport Automatically mark variables and functions which are modified or created for export to the environment of subsequent commands. By default all user created variables are local unless exported using the export command. If turned on this variable will export all variables and functions to subshell.
braceexpand The Bash shell performs brace expansion. You can turn on or off this shell feature. This is on by default.
emacs Use an emacs-style command line editing interface. This is enabled by default when the shell is interactive, unless the shell is started with the --noediting option.
errexit Exit immediately if a simple command exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a && or ││ list, or if the command's return value is being inverted via !. A trap on ERR, if set, is executed before the shell exits.
errtrace If set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a subshell environment. The ERR trap is normally not inherited in such cases.
functrace If set, any traps on DEBUG and RETURN are inherited by shell functions, command substitutions, and commands executed in a subshell environment. The DEBUG and RETURN traps are normally not inherited in such cases.
hashall Remember the location of commands as they are looked up for execution. This is enabled by default.
histexpand Enable ! style history substitution. This option is on by default when the shell is interactive.
ignoreeof Ignore <CTRL+d> which is used to leave the shell, (see IGNOREEOF variable).
history Enable command history. This option is on by default in interactive shells.
monitor Monitor mode. Job control is enabled. This option is on by default. Background processes run in a separate process group and a line containing their exit status is printed upon their completion.
noclobber If set, bash does not overwrite an existing file with the >, >&, and <> redirection operators. This may be overridden when creating output files by using the redirection operator >| instead of >.
noexec Read commands but do not execute them. This may be used to check a shell script for syntax errors (see debug a script section). This is ignored by interactive shells.
noglob Enable or disable pathname expansion.
notify Report the status of terminated background jobs immediately, rather than before the next primary prompt. This is effective only when job control is enabled.
nounset Treat unset variables as an error when performing parameter expansion. If expansion is attempted on an unset variable, the shell prints an error message, and, if not interactive, exits with a non-zero status.
onecmd Exit after reading and executing one command.
physical If set, the shell does not follow symbolic links when executing commands such as cd that change the current working directory. It uses the physical directory structure instead. By default, bash follows the logical chain of directories when performing commands which change the current directory.
pipefail If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.
posix Change the behavior of bash where the default operation differs from the POSIX standard to match the standard (posix mode).
privileged Turn on or off privileged mode. In this mode, the $ENV and $BASH_ENV files are not processed, shell functions are not inherited from the environment, and the SHELLOPTS variable, if it appears in the environment, is ignored. If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, these actions are taken and the effective user id is set to the real user id. If the -p option is supplied at startup, the effective user id is not reset. Turning this option off causes the effective user and group ids to be set to the real user and group ids.
verbose Print shell input lines as they are read.
vi Use a vi-style command line editing interface.
xtrace After expanding each simple command, for command, case command, select command, or arithmetic for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list.

You must refer to Bash man page for up to date information regarding the above variables:

man bash