Monday, 22 February 2021

Linux Set Command

 Linux set command is used to set and unset certain flags or settings within the shell environment. These flags and settings determine the behavior of a defined script and help in executing the tasks without facing any issue. The values of shell attributes and parameters can be changed or displayed by using the set command.

Syntax:

  1. set [options]  

To know about how to set the environment variable in Linux, visit Here.

Options: The supported options by the set command are as following:

  • -a: It is used to mark variables that are modified or created for export.
  • -b: It is used to notify of job termination immediately.
  • -e: It is used to exit immediately if a command exits with a non-zero status.
  • -f: It is used to disable the file name generation (globbing).
  • -h: It is used to save the location of commands where they looked up.
  • -k: It is used to place all assignment arguments in the environment variable of a command, except those that precede the command name.
  • -m: It is used to enable Job control.
  • -n: It is used to read commands.
  • -o: It is used for option-name.
  • -p: It is used to disable the processing of the '$ENV' file and import shell functions. It is turned on whenever the real and effective user ids do not match. Turning off this option may cause the working uid and gid to be set as the authorized uid and gid.
  • -t: It is used to exit from the command after executing one command.
  • -u: It is used to treat unset variables as an error when substituting.
  • -v: It is used to print shell input lines.
  • -x: It is used to print commands and their arguments in a sequential way (as they are executed).
  • -B: It is used to perform brace expansion by the Shell.
  • -C: It is used to disallow existing regular files to be overwritten by redirection of output.
  • -E: It is used if the ERR trap is inherited by the shell functions.
  • -H: It is used to enable style history substitution. By default, it is on when the shell is interactive.
  • -P: It is used if we do not want to follow symbolic links when executing commands.
  • -T: If this flag is set, the DEBUG trap is inherited by the shell functions.

Note: To turn off the above flags, use the '+' rather than '-' symbol.

To better understand the set command, let's see a brief introduction of the shell variable:

Shell Variable

A variable is a character string that can hold a value. The assigned value could be anything such as number, filename, text, or any other data type. It is just like a pointer to the actual data. The shell allows us to create, delete, and assign variables.

The variable name can have any letters, numbers, or underscore (_) character. We cannot have the characters such as "! *, or -", because these special characters have other meanings for the shell. According to Unix naming convention, the Unix shell variable must have their names in UPPERCASE.

Examples of the set command

Turn debugging information on or off.

The '-x' option is used with the set command to show command and their arguments. It is useful for debugging the shell script.

Execute the following command:

To turn on the debugging information:

  1. set -x  

To turn off the debugging information:

  1. set +x  

Disable Bash's default behavior.

To disable the default behavior of Bash, execute the command as follows:

  1. set -C  

Stop a script immediately.

To stop a script immediately, execute the following command:

  1. set -e  

Getting Help

If you get stuck during the use of the set command, you can access the help documentation from your terminal. To access the help manual, execute the below command:

  1. set --help  

No comments:

Post a Comment