0

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.

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.