Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the 'export' command do?

I happen to run some commands blindly, in order to get things done.

I started to work with Jenkins recently, and then I had to use this export command to run the Jenkins WAR archive. What does the export command do in general, and why do we need to run this command, while running Jenkins (after the Jenkins home is set)?

like image 659
Chathura Kulasinghe Avatar asked Dec 10 '12 05:12

Chathura Kulasinghe


People also ask

What is export command windows?

The export combined with the import command allows you to batch install applications on your PC. The export command is often used to create a file that you can share with other developers, or for use when restoring your build environment.

What does export command do in Mac terminal?

The export command is fairly simple to use as it has straightforward syntax with only three available command options. In general, the export command marks an environment variable to be exported with any newly forked child processes and thus it allows a child process to inherit all marked variables.

What does export do in Ubuntu?

export is a command in the Bash shell language. When used to set a variable, as in your example, the variable (PATH) will be visible ("exported to") any subprocesses started from that instance of Bash. Without the export command, the variable will not exist in the subprocess.

What does export mean in Python?

export is a command that you give directly to the shell (e.g. bash ), to tell it to add or modify one of its environment variables. You can't change your shell's environment from a child process (such as Python), it's just not possible.


2 Answers

export in sh and related shells (such as Bash), marks an environment variable to be exported to child-processes, so that the child inherits them.

export is defined in POSIX:

The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. If the name of a variable is followed by = word, then the value of that variable shall be set to word.

like image 72
Yann Ramin Avatar answered Sep 23 '22 23:09

Yann Ramin


I guess you're coming from a Windows background. So I'll contrast them (I'm kind of new to Linux too). I found a 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., the documentation for the set command in Windows "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, but 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 it 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 QnAs:

  • 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 (by the way, 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.

like image 29
barlop Avatar answered Sep 22 '22 23:09

barlop