Shell Scripting - Set Command
Last Updated :
03 Jul, 2024
The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugging. Understanding the `set` command is essential for enhancing the flexibility and robustness of shell scripts, making it a fundamental skill for anyone working with Unix-like operating systems.
What is a set Command in Linux?
It is used to set or unset specific flags and settings ( determines the behavior of the script and helps in executing the tasks without any issues) inside the shell environment. It can be used to change or display the shell attributes and parameters.
Syntax
set -options arguments
Options of Set command in Linux
- The set command supports the following options:
Option | Description |
---|
-a | use to mark variables that are created or modified or created for export. |
-b | use to notify the termination of the job. |
-e | use to exit when the command exits with a non-zero status. |
-f | it disables the file name generation known as globbing |
-h | It saves the location of the command where it got looked. |
-k | It places all assignment arguments in the environment variable of a command. Exception: It excludes all arguments that precede the command name. |
-m | It enables job control |
-n | It is used to read the commands. |
-o | It is used for naming the option |
-p | It disables the processing of the '$ENV' file and also imports the shell functions. Turned on: when the real and effective user ids do not match. |
-t | It uses to exit from the command after executing a single command |
-u | It treats unset variable as an error during substitution |
-v | It prints the shell input lines |
-x | It prints the commands and their arguments in the same sequence as they got executed. |
-B | It performs the Brace expansion by the shell |
-C | It disallows the existing regular files to be overwritten |
-E | used when shell functions inherit the ERR trap |
-H | It enables style history substitution. It is on by default. |
-P | used when during command execution we don't want to follow the symbolic links. |
-T | set this flag, this helps shell functions to inherit the DEBUG trap |
Usage and Examples of Set Command in Shell Scripting
The following are the some of the examples of set command in shell scripting:
Set -x command
- This option prints the commands in the sequence as they got executed or is mainly used to do some script debugging.
Code
set -x
echo Hello
echo Romy
Output
bar
hello
Romy
+ echo bar
+ echo hello
+ echo Romy

- From the above example, We can see the commands getting printed after the command execution with the '+' sign.
Set -e command
- It terminates the execution when the error occurs.
Code:(without set -e)
echo Hello
foo
echo Romy
Output
Hello
Romy
main.sh: line 14: foo: command not found

- 'foo' is a non-existent command but bash still executed the third line after encountering the error at the second line. We can use the set command to stop termination.
Code: (with set -e)
set -e
echo Hello
foo
echo Romy
Output
Hello
main.sh: line 15: foo: command not found
- We can see that the third line is not getting printed as the execution got terminated after the second line.
Pipe commands
- The Set -e command does not work with piped commands.
Example
set -e
foo | echo " This is the piped command"
echo "executed"
Output
This is the piped command
executed
main.sh: line 3: foo: command not found

- We can see that third line is getting executed, instead of terminating the execution after the second line.
- To overcome this problem, we have to use 'set -eo pipefail'
Set -eo pipefail
Example
set -eo pipefail
foo | echo " This is the piped command"
echo "executed"
Output
This is the piped command
main.sh: line 13: foo: command not found

Set positional parameters using set command
It can be used to assign values to positional parameters. Position of the value referenced as ${N} where N denotes the position of the parameter.
The $1 is the first positional parameter after the command. The $2 value is the second parameter, and so on.
Example
set apple mango orange guava
echo $1
echo $2
echo $3
echo $4
Output
apple
mango
orange
guava

Unset positional parameters
In order to unset the positional parameter, run the set command followed by two hyphens(set --).
Example
set apple mango orange guava
set --
echo $1
echo $2
echo "Hello"
Output
Hello

- We can see that nothing is getting printed corresponding to the first two commands to print positional parameters.
Features of set command
The following are the features of set command:
- Environment Variable Management: It allows users to display, set, or unset environment variables in the command shell.
- Control Shell Behavior: It can modify shell options and control the behavior of the command interpreter.
- Script Debugging: Enables or disables debugging features to assist in script development and troubleshooting.
- Conditional Execution: Helps in the conditional execution of commands by setting options that affect command processing.
Best practices of using set Command
The following are the some of the best practices of using set command in shell scripting:
- Enable Script Debugging: The
set -x
commadn is used to provide the facility of enabling debugging mode feature, which helps in displays each command as it is executed. It helps in tracing and diagnosing the script errors and unexpected behaviors. - Strict Error Handling: The
set -e
helps in adopting to handle and enable strict error handling mode, which causing the script to exit immediately if any command exits with a non-zero status. This helps in ensures that errors are caught early and script execution stops upon encountering critical failures. - Positional Parameters Management: On utilizing the
set
with parameters, we can manage the positional parameters explicitly. This clears any previous arguments, allowing for reliable handling and parsing of command-line arguments passed to the script.
Benefits and Usecases of using Set command
The following are the benefits and usecases of using the set command:
- Enhanced Error Handling: The
set -e
command helps in ensuring the immediate script termination when it is encountering errors. It helps in improving script reliability and preventing partial execution issues. - Debugging Capabilities: This
set -x
facilitates with enablling the tracing of script execution, supporting in debugging by displaying each command before execution, helping to identify and resolve issues quickly. - Parameter and Environment Management: The
set
commands like set is assist as useful
for managing positional parameters and set -u
for treating unset variables as errors streamline script development, ensuring predictable behavior across different environments. - Improved Script Portability: On utilizing the
set +o
options allows consistent control over shell behavior, enhancing script compatibility and making maintenance easier across various Unix-like operating systems.