Skip to content

set command in Linux

The set command in Linux is used to set or unset shell options and positional parameters. It can enable options like -x for debugging or -e for exiting immediately when a command returns a non-zero status. By setting positional parameters, you can assign values to variables within a script. This command is essential for customizing the behavior of your shell environment and controlling the flow of your scripts.

set Syntax:

Terminal window
set [option] [parameter]

Linux set Options:

OptionDescription
-eExit immediately if a command exits with a non-zero status.
-uTreat unset variables as an error when substituting.

set Parameters:

ParameterDescription
variableSpecifies the variable to be set.

How to use set command:

Set a Bash Variable

Terminal window
fav_color=blue

Defines a Bash variable named “fav_color” with the value “blue”.

List All Variables

Terminal window
set

Displays all variables, functions, and positional parameters in the current shell session.

Set an Array Variable

Terminal window
fruits=(apple orange banana)

Defines an array variable named “fruits” containing three elements: apple, orange, and banana.

Set a Variable with Command Output

Terminal window
current_date=$(date)

Assigns the output of the “date” command to a variable named “current_date”.

Set Read-Only Variable

Terminal window
readonly user="john"

Defines a read-only variable named “user” with the value “john”, preventing it from being changed.

Unset a Variable

Terminal window
unset fav_color

Removes the variable “fav_color” from the shell session.

Export a Variable

Terminal window
export PATH=$PATH:/opt/bin

Adds “/opt/bin” to the PATH variable, making it accessible to all child processes.

Set a Default Value for an Unset Variable

Terminal window
echo ${name:-"Anonymous"}

Prints the value of the variable “name,” or “Anonymous” if the variable is unset or null.

How do I use set in Linux?

To use the set command in Linux, execute the following command:

Terminal window
set --option <value>

How do I enable debugging with set in bash?

To enable debugging with the set command in bash, use the following command:

Terminal window
set -x

How do I show all shell input with set in Linux?

To display all shell input using the set command in Linux, run:

Terminal window
set -v

How do I check if a variable is unset with set in bash?

To check whether a variable is unset using the set command in bash, you can use:

Terminal window
set -u

How do I use the noclobber option with set in Linux?

To prevent overwriting existing files with the noclobber option using the set command in Linux, utilize:

Terminal window
set -o noclobber

How do I ignore the errexit setting with set in bash?

To ignore the errexit setting using the set command in bash, you can apply:

Terminal window
set +e

How do I view current shell options with set in Linux?

To view the current shell options using the set command in Linux, type:

Terminal window
set -o

How do I disable tracing with set in bash?

To disable tracing when using the set command in bash, input the following command:

Terminal window
set +x

How do I find the current value of a specific option with set in Linux?

To determine the current value of a specific option using the set command in Linux, you can check by running:

Terminal window
shopt -o <option>

Applications of the set command

  • Managing shell options and attributes
  • Displaying or modifying shell variables and functions