set

>Usage

The set command in Unix is used to display or change shell options and variables. When used without any arguments, it lists all shell variables and functions.

You can also use set to enable or disable shell options, which control certain shell behaviors.

>Synopsis

set [option] [argument]

>Options

-e

Exits the shell immediately if any command has a non-zero exit status, except in certain cases, for example if commands are in loops or conditionals.

-o

Enable specific shell options that affect the behavior of the shell.

Example 1:

Dots

-zsh

> set -o noclobber


This option prevents overwriting of files when using redirection (>). If you try to redirect output to a file that already exists, the shell will raise an error.

+o

Disable specific shell options that affect the behavior of the shell. Is the opposite of -o option.

-x

Enables debugging mode, the shell prints each command and its arguments to the standard output before executing it.

-u

Treats unset variables as an error, when this option is enabled, if you try to use a variable that hasn’t been assigned a value the shell will immediately terminate the script and display an error message.

Example 2:

Dots

-zsh

> set -u

> echo $VAR


bash: VAR: unbound variable

When set -u is enabled, attempting to use an unset variable results in the shell throwing an error

>Quiz

Progress: Question ? out of ?

Question Title

Page written by Alessandro Tognoni