What does the 'export' command do?

前端 未结 3 564
被撕碎了的回忆
被撕碎了的回忆 2020-12-22 18:42

I am a little new to Linux, and I happen to run some commands blindly, in order to get things done. I thought that it will not be a waste of asking these type of questions,

3条回答
  •  暖寄归人
    2020-12-22 18:58

    I guess you're coming from a windows background. So i'll contrast them (i'm kind of new to linux too). I found user's reply to my comment, to be useful in figuring things out.

    In Windows, a variable can be permanent or not. The term Environment variable includes a variable set in the cmd shell with the SET command, as well as when the variable is set within the windows GUI, thus set in the registry, and becoming viewable in new cmd windows. e.g. documentation for the set command in windows https://technet.microsoft.com/en-us/library/bb490998.aspx "Displays, sets, or removes environment variables. Used without parameters, set displays the current environment settings." In Linux, set does not display environment variables, it displays shell variables which it doesn't call/refer to as environment variables. Also, Linux doesn't use set to set variables(apart from positional parameters and shell options, which I explain as a note at the end), only to display them and even then only to display shell variables. Windows uses set for setting and displaying e.g. set a=5, linux doesn't.

    In Linux, I guess you could make a script that sets variables on bootup, e.g. /etc/profile or /etc/.bashrc but otherwise, they're not permanent. They're stored in RAM.

    There is a distinction in Linux between shell variables, and environment variables. In Linux, shell variables are only in the current shell, and Environment variables, are in that shell and all child shells.

    You can view shell variables with the set command (though note that unlike windows, variables are not set in linux with the set command).

    set -o posix; set (doing that set -o posix once first, helps not display too much unnecessary stuff). So set displays shell variables.

    You can view environment variables with the env command

    shell variables are set with e.g. just a = 5

    environment variables are set with export, export also sets the shell variable

    Here you see shell variable zzz set with zzz = 5, and see it shows when running set but doesn't show as an environment variable.

    Here we see yyy set with export, so it's an environment variable. And see it shows under both shell variables and environment variables

    $ zzz=5
    
    $ set | grep zzz
    zzz=5
    
    $ env | grep zzz
    
    $ export yyy=5
    
    $ set | grep yyy
    yyy=5
    
    $ env | grep yyy
    yyy=5
    
    $
    

    other useful threads

    https://unix.stackexchange.com/questions/176001/how-can-i-list-all-shell-variables

    https://askubuntu.com/questions/26318/environment-variable-vs-shell-variable-whats-the-difference

    Note- one point which elaborates a bit and is somewhat corrective to what i've written, is that, in linux bash, 'set' can be used to set "positional parameters" and "shell options/attributes", and technically both of those are variables, though the man pages might not describe them as such. But still, as mentioned, set won't set shell variables or environment variables). If you do set asdf then it sets $1 to asdf, and if you do echo $1 you see asdf. If you do set a=5 it won't set the variable a, equal to 5. It will set the positional parameter $1 equal to the string of "a=5". So if you ever saw set a=5 in linux it's probably a mistake unless somebody actually wanted that string a=5, in $1. The other thing that linux's set can set, is shell options/attributes. If you do set -o you see a list of them. And you can do for example set -o verbose, off, to turn verbose on(btw the default happens to be off but that makes no difference to this). Or you can do set +o verbose to turn verbose off. Windows has no such usage for its set command.

提交回复
热议问题