53

What is the difference between set, env, declare and export when setting a variable in a Linux shell, such as bash?

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
user3872279
  • 1,085
  • 2
  • 9
  • 18

2 Answers2

40

First, you must understand that environment variables and shell variables are not the same thing.

Then, you should know that shells have attributes which govern how it works. These attributes are not environment nor shell variables.

Now, on to answering your question.

  1. env: without any options, shows current environment variables with their values; However can be used to set environment variable for a single command with the -i flag
  2. set: without options, the name and value of each shell variable are displayed* ~ from running man set in rhel; can also be used to set shell attribute. This command DOES NOT set environment nor shell variable.
  3. declare: without any options, the same as env; can also be used to set shell variable
  4. export: makes shell variables environment variables

In short:

  1. set doesn't set shell nor environment variables
  2. env can set environment variables for a single command
  3. declare sets shell variables
  4. export makes shell variables environment variables

NOTE declare -x VAR=VAL creates the shell variable and also exports it, making it environment variable.

Tran Triet
  • 531
  • 4
  • 8
  • 2
    What about `declare -g` this will make a global variable, will it also be part of the environment? – Mr. Roland Mar 10 '20 at 14:44
  • 2
    It won't be part of the environment. It will become a shell variable that has global scope. To see the `global scope` effect, you can try create a function which does `declare -g a=3`. Once you run that, since the variable is globally scoped, even after you have exited the function, the variable still exist (e.g. you can reference it with echo $a) – Tran Triet Mar 10 '20 at 16:49
  • 3
    So `declare -x` is almost the same as `export` according to https://stackoverflow.com/q/5785668/322020 – Nakilon Aug 24 '20 at 01:10
  • 2
    *`set` doesn't set shell nor environment variables* ---- really misleading design... – smwikipedia Sep 20 '21 at 07:59
  • Is there a way to display all `shell attributes` for current shell session? – smwikipedia Sep 20 '21 at 08:02
  • https://unix.stackexchange.com/questions/210158/how-can-i-list-bashes-options-for-the-current-shell - *note* here I called it `shell attribute` but maybe the better term is `shell option` as used in the referenced post. – Tran Triet Oct 01 '21 at 05:15
  • great answer. Would be perfect with some links to further detailed explaination about e.g. shell attributes ("[shell parameters](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html)" ?), shell variables, ... – kca Oct 08 '21 at 10:26
  • What about `unset`, does this remove environment or shell variables? – v010dya Aug 07 '22 at 11:56
  • More clarrification req'd? Still confusion: Partly as `set` in other shells not the same & bash has more than one var type (& set displays variables). Bash makes a distinctions by type:- *environment*:`env`|`export`|`set`/ *options*:`shopt (-o)`|`set` / *parameters*:`=`|`set`/ *variables*:(reserved vars)/ *attributes*:`declare`. Environment is a var that is passed to subshells. *Attributes* are vars + some extra metadata. *Variables* are vars with specific use and `export` alters use/scope of vars. *Options* available with `shopt` are extended with `set` and `set -k` can pass vars. – Jay M Apr 09 '23 at 18:14
  • I could not abbreviate more and still fit that all in. :/ Perhaps I should have just extended your answer! So, anyway I had to drop `let` and `local` but they are less to do with definition, more behaviour. – Jay M Apr 09 '23 at 18:18
10

It seems that set and declare are slightly different, with set being more powerful.

See "declare" under https://www.gnu.org/software/bash/manual/bash.html#Bash-Builtins declare: "Declare variables and give them attributes. If no names are given, then display the values of variables instead.

Set "set" under https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin * set: "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."

ENV is an environment variable in Bash: https://www.gnu.org/software/bash/manual/bash.html#Bash-Variables env is a Linux command. I think this is a good reference: https://unix.stackexchange.com/questions/103467/what-is-env-command-doing

I thought this was a good explanation of export: http://www.unix.com/302531838-post2.html

Also: https://www.gnu.org/software/bash/manual/bash.html#Bourne-Shell-Builtins * export (from Bourne): "Mark each name to be passed to child processes in the environment."

Borrowing code from URL above:

root@linux ~# x=5                <= here variable is set without export command
root@linux ~# echo $x
5
root@linux ~# bash               <= subshell creation
root@linux ~# echo $x            <= subshell doesnt know $x variable value
root@linux ~# exit               <= exit from subshell
exit
root@linux ~# echo $x            <= parent shell still knows $x variable
5
root@linux ~# export x=5         <= specify $x variable value using export command
root@linux ~# echo $x            <= parent shell doesn't see any difference from the first declaration
5
root@linux ~# bash               <= create subshell again
root@linux ~# echo $x            <= now the subshell knows $x variable value
5
root@linux ~#
Shawn P.
  • 117
  • 1
  • 5