What is the difference between set, env, declare and export when setting a variable in a Linux shell, such as bash?
- 153,128
- 77
- 353
- 394
- 1,085
- 2
- 9
- 18
2 Answers
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.
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-iflagset: without options, the name and value of each shell variable are displayed* ~ from runningman setin rhel; can also be used to set shell attribute. This command DOES NOT set environment nor shell variable.declare: without any options, the same asenv; can also be used to set shell variableexport: makes shell variables environment variables
In short:
setdoesn't set shell nor environment variablesenvcan set environment variables for a single commanddeclaresets shell variablesexportmakes shell variables environment variables
NOTE
declare -x VAR=VAL creates the shell variable and also exports it, making it environment variable.
- 531
- 4
- 8
-
2What 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
-
2It 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
-
3So `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
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 ~#
- 117
- 1
- 5
-
-
I got to downvote this because it's simply not answering the question. – Daniel C. Sobral Jun 11 '18 at 17:23
-