Shell Scripting - Set Command - GeeksforGeeks

04 Jan, 2022
In this article, we will see the SET command in bash scripting.
Set command: 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 issue.) inside the shell environment. It can be used to change or display the shell attributes and parameters. 
set -options arguments
The set command supports the following options:
It places all assignment arguments in the environment variable of a command.
Exception: It excludes all arguments that precede the command name.
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.
For a demonstration of the use of the set command, let’s use some set 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
We can see the commands getting printed after the command execution with the ‘+’ sign.
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.
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’
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
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
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.
Picked
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
scp command in Linux with Examples
nohup Command in Linux with Examples
mv command in Linux with examples
Thread functions in C/C++
Docker - COPY Instruction
chown command in Linux with Examples
nslookup command in Linux with Examples
SED command in Linux | Set 2
Named Pipe or FIFO with example C program
uniq Command in LINUX with examples