Updated April 10, 2023
Introduction to Shell Script Set
Shell scripting has been an essential tool for everyday work in terms of setting up a cron job or carrying out some repeated set of commands to accomplish a task. In a Linux OS, shell script can be also used as a root user to complete required action at a system level as well. Manipulating files, executing routine backups, system monitoring are some instances where shell scripts are used day in and day out. When it comes to setting the shell parameters, there is set for you.
What is Shell Script Set?
In shell scripting a user might need to change the shell options or the positional parameters or assist in displaying the names and values of the variables of shell using the same and that is where one would closely need the help of set command.
The syntax of set command is as follows:
set [--xvutpnmkhfebaTPHECB][-o <option name>][argument]
All the options in “–” and “-” includes various flexibility to the set command to perform all kinds of jobs it is assigned in day to day work. In subsequent paragraphs we would look at each of these options which are available with set command in great detail and also look into why those options are used and under what circumstances one should use them.
Lets start with the order in which the syntax is written starting with x:
- –x: Using this option, one can print the trace for simple commands, for commands, case commands, select commands, for commands in case of arithmetic along with the argument structure it possess. It is one of the most useful features for debugging purpose.
- –v: This is again an option used for debugging purpose, where it enables the script to read lines as they execute. This is not that widely used in scripting as it is not that straight forward to trace back to the root cause of a bug as –x is.
- –u: In bash when there is requirement of parameter expansion, this option is used for treating any unset parameter except the “@” and “*” as an error. The error message will be presented on the screen before exiting of the shell.
- –t: In highly unlikely cases of reading only and single command and exiting post that this command is used. This is not that widely used as scripts are written to execute multiple commands and having this option kills the motive of having a shell script. In case of a script to break, this option is highly used.
- –p: This option enables the privilege mode. Privileged mode is a way of increasing the security in way by restricting any potentially dangerous actions from execution. It reduces the chances the attack by giving an “under-privilege” access so that any action can’t trick the linux to perform some erroneous task.
- –n: This option is mainly used for checking the syntax before actually executing the commands. Here in this option the commands are not executed, but only read.
- –m: Using this, one can enable job control. In case of running of any process, they are executed in separate groups and with this option one can control the jobs by having the ability to pause for a certain time and restart again.
- –k: Using this options allows all assignment statements to be placed in environment.
- –h: This is an option which is enabled by default where the commands are located and remembered as they are looked up for execution.
- –f: This option allows to disable any filename expansion.
- –e: This option enables any pipeline, consisting of single simple command or a list or a compound command has returned a non-zero status.
- –b: Any terminated background jobs is immediately reported using this option in a shell script.
- –a: This option enables the user to give export attribute to each and every variable or function allowing them to be marked for export for subsequent commands to the environment.
- –T: This allows any trap in DEBUG and RETURN to be inherited by shell functions or commands.
- –P: This option doesn’t allow symbolic links to be resolved when set. For example, while performing directory change using cd physical directory is used when option is set.
- –H: This option allows history substitution in ‘!’ style. Most of interactive shell has this set by default.
- –E: Like –T, this option of –E allows trap in ERR to be inherited by shell function.
- –C: This option is mostly used to avoid overwriting any existing files, when the output redirection is used using ‘>’, ‘>&’ and ‘<>’.
- –B: This enable the shell to perform Brace expansion. Brace expansion is nothing but a mechanism to generate arbitrary expression.
Examples of Shell Script Set
Given below are the examples:
Example #1
In our example we would primarily see how one can use set command if the user forgets to assign any one argument through the command line. At first, let us not send any argument and in the script we would set the time and get all the arguments from the date itself.
In Linux, date is written as Tue May 5 20:12:29 UTC 2020.
Here the chronological order of variables is:
1: Tue
2: May
3: 5
4: 20:12:29
5: UTC
6: 2020
In the first execution, we won’t send any argument, and the system will automatically take the variables from our script and show it correspondingly.
Output:
Example #2
In the next set of example we would provide all arguments except the time, and let us see what output we get from the same. Eventually, we should be getting the same output as above because our code uses set command if number of arguments are not equal to 6.
Output:
As soon as the number is equal to 6, we would use the arguments passed through command line the output will be as per the arguments as well.
Output:
Conclusion
In conclusion, we now know that there are innumerable number of options to use set command in everyday execution of shell script and it entirely depends on the problem we are trying to solve. For example, if the script demands more security one would prefer to use –p option and so on. You are welcome to use these commands and options in your script and see the wonders the set command brings to your demand of results from the script. On an ending note, we would like to say that this command is a treasure unexplored and has a lot of things to bring to the table for script execution.
Recommended Articles
We hope that this EDUCBA information on “Shell Script Set” was beneficial to you. You can view EDUCBA’s recommended articles for more information.