What does set command without arguments do?

set is a shell built-in that displays all shell variables, not only the environment ones, and also shell functions, which is what you are seeing at the end of the list.

Variables are displayed with a syntax that allow them to be set when the lines are executed or sourced.

From bash manual page:

If no options or arguments are supplied, set displays the names and values of all shell variables and functions, sorted according to the current locale, in a format that may be reused as input for setting or resetting the currently-set variables.

On different shells, the behavior is not necessarily the same; for example, ksh set doesn't display shell functions.


set is a shell builtin, used to set and unset shell options and positional parameters.

Without arguments, set will print all shell variables (both environment variables and variables in current session) sorted in current locale.

You can also read bash documentation.


There're a few caveats.

set "$var" will assign var value to $1. If $var starts with - or +, then $var content will be treated as sequences of shell options. If $var contains any invalid options, most POSIX shells will print the error. yash and zsh in sh, ksh emulation are not only printing the error, but also setting valid options. While yash stops setting options on the first invalid option, zsh will assign all of them. In yash:

var=-fxd; set "$var"

f and x will be present in $-, while:

var=fdx; set "$var"

only f is present in $-. In both cases, f and x will be present in $- with zsh in sh and ksh emulation.

To protect you from that situation, you can pass -- as the first argument to set a positional parameter even if it starts with - or +:

var=-fdx; set -- "$var"

will assign $var to $1, regardless of its content.

set -- without any further arguments will unset all positional parameters.

If the first argument is -, the behavior is unspecified. All known POSIX shells will unset x and v options (except posh), and assign anything after - to positional parameters:

set -xv - -f

will assign -f to $1. set - also did not unset positional parameters. Schily osh also behaves like that. Heirloom sh does not unset v and x options.

The only POSIX shell exception is yash, which treats - as the first positional parameter:

$ yash -c 'set -xv - -f; printf "%s\n" "$@"; printf "%s\n" "$-"'
+ printf %s\n - -f
-
-f
+ printf %s\n cvx
cvx

Schily sh even doing nothing if - is present in arguments:

$ schily-sh -c 'set -v - -f; printf "%s\n" "$@"; printf "%s\n" "$-"'
<blank line>
s

$ schily-sh -c 'set -v -- -f; printf "%s\n" "$@"; printf "%s\n" "$-"'
-f
vs

Because set has output and which set returns nothing you know it's part of the shell you are using, likely bash.

man bash mentions it many times but it's easier to link to in the online documentation.

This builtin is so complicated that it deserves its own section. set allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables.

set has the call specification of:

set [--abefhkmnptuvxBCEHPT] [-o option-name] [argument …]
set [+abefhkmnptuvxBCEHPT] [+o option-name] [argument …]

so if you want to find it in man on the command line you can search with

/set.*abef

Tags:

Bash

Set