21

What does set command without arguments do?

As I can see it prints out my environment variables just like env command but in alphabetical order. And further it prints some different information (variables? functions?) like:

__git_printf_supports_v=yes
__grub_script_check_program=grub-script-check
...
quote () 
{ 
    local quoted=${1//\'/\'\\\'\'};
    printf "'%s'" "$quoted"
}
quote_readline () 
{ 
    local quoted;
    _quote_readline_by_ref "$1" ret;
    printf %s "$ret"
}

What is it and where it comes from?

I cannot find information about set command without arguments. Actually I don't have man page for set in my Linux distribution at all.

  • 1
    "This builtin is so complicated that it deserves its own section." See the docs – galath Feb 17 '17 at 10:16
16

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.

  • 3
    That's because ksh do not store function definition in shell variable. – cuonglm Jan 15 '16 at 18:16
  • 2
    @cuonglm A wise design decision made by dgk! ;-) – jlliagre Jan 15 '16 at 22:31
7

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
  • which set depends on your shell. At least zsh will report it for you. You can also read this for more details. – cuonglm Jan 16 '16 at 3:50
  • It's unlikely the question is about anything but bash as it has a large market dominance, and anyone using the non default shell would not need to ask this question. – user1133275 Jan 17 '16 at 4:56
6

set is a shell builtin, used for 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 start with - or +, then $var content was treated as sequences of shell options. If $var contain 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 stop setting options on the first invalid option, zsh will assign all of them. In yash:

var=-fxd; set "$var"

f and x will present in $-, while:

var=fdx; set "$var"

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

To protect you from that situation, you can pass -- as the first argument to set positional parameter even it start 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 behave like that. Heirloom sh do not unset v and x options.

The only POSIX shell exception is yash, which treat - 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 - 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
  • i never understand why people would link to anywhere but the first one... you might like to mention the different between set -- $unset, set - $unset, set $unset, and set "$unset" and so on - it's usually those discrepancies that get people into that kind of trouble in the first place. – mikeserv Jan 15 '16 at 19:02
  • @mikeserv: Well, I added further details, but I didn't got what do you mean when used $unset. – cuonglm Jan 16 '16 at 3:27
  • @cuonglm The command set $unset will act exactly equal as set for an unset variable. But set "$unset" will do noting, if unset is actually an unset variable. A set - $unset removes the -v and -x set options (in bash). – user79743 Jan 16 '16 at 8:41
  • @BinaryZebra: Ah, of course, for me, it's better to use unset_var. And also note that set "$unset" is actually set $1 to empty. That's about shell expansion, not strictly to set, so I don't mention it. – cuonglm Jan 16 '16 at 9:50
  • 1
    @mikeserv: The behavior of posh and yash are different when - appear. You can read my updated (and feel free to improve it). – cuonglm Jan 16 '16 at 17:42
2

If you are working inside a bash shell, type help set.

At the end of the help printed, read:

If no ARGs are given, all shell variables are printed.

So, a no arguments set prints all variables known to the shell.

  • I wonder if set -x; set would be helpful – ILMostro_7 Jan 16 '16 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.