Using Linux set command


Set in Linux is a shell built-in command it is used to set shell and environment variables. You can change and display the values of shell attributes and parameters using the set command.

In this article, we will discuss what is set command in Linux and its usage along with some examples.

The syntax of the Linux set command

The basic syntax of how to use the Linux set command is given below.

set [options]  arguments

The frequently used options are:

-a – This option is used to mark variables that are modified or created for export.

-b – Using this will give notification of job termination immediately.

-e – Exit immediately if a command exits with a non-zero exit status.

-f – This will disable the file name generation (globbing).

-h – This option is enabled by default. It locates and then remembers a function as it is awaiting execution.

-k – All keyword arguments are placed in the environment for a command, not only those that precede the command name.

-n – It is used to read commands but does not executes.

-t – This option is used to exit by reading and executing one command.

-u – The -u option treats unset or undefined variables as errors except for special parameters such as wildcards (*) or “@” during parameter expansion.

-v – Prints shell input lines as they are read.

-x – This option is used to print commands and their arguments in a sequential way (as they are executed).

Usage of set command in Linux

Some examples of using the set command in Linux are given below.

Turn on or off debugging information

The option x with set command help in debugging shell scripts.

To turn on the debugging information use:

set -x

To turn off the debugging information use:

set +x

Stop a script immediately

You can use the given command to stop the execution of a script immediately.

set -e

Set positional parameters

We can use the set command to assign values to positional parameters. A positional parameter is a variable in shell its value is referenced as $N where N is a digit denoting the position of the parameter.

For example –

set var1 var2 var3

To list these positional parameters use the given command:

For the first positional parameter use –

echo $1

For second parameter run –

echo $2

Similarly, you can print other parameters also.

To print all the parameters in order $1$2$3 use-

echo $*

Unset all the positional parameters

To unset the positional parameters, use --(double hyphen) with the set command.

set --

Now if you try to print the values of these positional parameters you will get blank output.

Conclusion

There are many more examples on how to use set command in Linux. Now if you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.