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

  • 1
    Why was this question voted down?
    – l0b0
    Nov 21 '14 at 15:45

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.

  • 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
  • 1
    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
  • 1
    So declare -x is almost the same as export according to stackoverflow.com/q/5785668/322020
    – Nakilon
    Aug 24 '20 at 1:10
  • set doesn't set shell nor environment variables ---- really misleading design... Sep 20 at 7:59
  • Is there a way to display all shell attributes for current shell session? Sep 20 at 8:02

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
root@linux ~# bash               <= subshell creation
root@linux ~# echo $x            <= subshell doesnt know $x variable value
root@linux ~# exit               <= exit from subshell
root@linux ~# echo $x            <= parent shell still knows $x variable
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
root@linux ~# bash               <= create subshell again
root@linux ~# echo $x            <= now the subshell knows $x variable value
root@linux ~#
  • declare and set and env? export vs declare?
    – Pacerier
    Nov 1 '17 at 19:52
  • I got to downvote this because it's simply not answering the question. Jun 11 '18 at 17:23
  • Let me know if this is any better.
    – Shawn P.
    Sep 4 '18 at 15:21

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.