In the Linux operating system, the ‘set’ command is a built-in shell command used to set and unset shell variables and options. It is commonly used in shell scripts to configure the environment and control the behavior of the script.
This guide clarifies everything about the set command of Linux, including the following.
- How Does the set Command Work in Linux?
- Set the value of Positional Parameters
- Unset the value of Positional Parameters
- Execute a Shell Script with an Error
Let’s get started by explaining the syntax of the set command!
How Does the set Command Work in Linux?
The set command of Linux helps the users to set or unset options or positional parameters that are helpful to determine the flow of the shell script. The syntax of the set command is explained below:
set -options arguments
There are numerous options of the set command in Linux that can be viewed using this command.
$ set --help
The most used options of the set command in Linux include the following.
Before moving to the options, you should know that the “-o” can enable and “+o” can disable the shell options. For example, we’d use this command to mark either created or modified variables and notify when the job is terminated (even if it was working in the background).
$ set -o allexport -o notify
When the “set” command is used without any options or parameters, it displays all the shell variables and their values, as seen below.
In the above image, the parameters are on the left side of the “=” sign, while on the right side, we see their values.
Set the Value of Positional Parameters
A positional parameter can be defined as the value in a shell script or program, and its values are assigned using the “set” command.
To understand it better, let’s execute this command first.
$ set Ubuntu Fedora LinuxMint Debian Kubuntu
The above image sets the values “$1,$2,$3,$4,$5”, where $1 stands for Ubuntu, $2 stands for Fedora, and so on.
If we are to list the third parameter, we will use this command.
$ echo $3
In the above image; we can see that it printed LinuxMint, which was set at position 3. If you want to list all the values of parameters, use this command.
$ echo “$*”
And it displays the values of all parameters from “$1 to $5”, as seen in the above image.
Unset the Value of Positional Parameters
The double hyphen ‘–‘ is used to unset the positional parameters, which is used in this format.
$ set --
The above command unsets the values of all parameters, which can be confirmed using this command.
$ echo “$*”
The above command, when executed, doesn’t display any parameter value because we’ve unset them.
Execute a Shell Script with an Error
If there’s a new script that you’ve written and you want to test it while you’re not sure where the error is, then using the set command, there’s a way to execute it until a line where the error is. Here’s a script that was created for an example.
#!/bin/bash set -e echo 'Welcome to itslinuxfoss.com' cat filename echo 'End of script'
In the above script, we’ve used the “set” command with the “-e” option, which is used to exit the script when there’s an error. Let’s execute this script we named “test.sh” and see the result.
$ bash test.sh
There are two echo commands in the above script. One is before the error, and the second is after the error, but only the first is executed because we’ve used the “-e” option of the set command to exit the script once it finds an error.
This article summarizes that the set command is used to set or unset the positional parameters or shell options. This is quite helpful in scripting as it allows the scriptwriters to write more effectively and efficiently using the various options. This guide has explored all the working/usage of the Linux set command.