Learner
Learner

Reputation: 21405

Export command in Unix

I am new to Unix and I know that the export command can be used for creating session variables but what is the purpose of the command below:

bash-3.2$ export $TEST
declare -x PWD="/home/username"
declare -x SHELL="/bin/bash"
declare -x SSH_TTY="/dev/pts/4"
declare -x USER="username"

Please help me in understanding this command. I don't have any variable with name TEST, but when I gave this command the shell printed some output.

Upvotes: 0

Views: 1135

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753725

The shell expanded the non-existent variable $TEST into an empty string, and then ran the command export with no arguments. When you do that, the shell lists the exported environment variables.

If you want to export an empty variable TEST, you should have written any of these:

export TEST
export TEST=
export TEST=''
export TEST=""

(and in this case, there are other ways to achieve the same effect, such as export TEST=$TEST or, more or less sanely, export TEST="$TEST", but that is normally abbreviated to export TEST.)

The detailed declare -x notation in the output is designed to allow you to take write the output to a file, and then reload the environment with the . (dot) or source commands. Different shells present the output differently, and not necessarily reusably.

The set command is similar (but more complex). It can be used to set shell options, to set the positional arguments ($1, $2, etc), but when run with no arguments it lists the set variables. Note that not every variable that is created is an exported environment variable.

Upvotes: 2

Related Questions