How to create Linux Bash shell variables

To create a shell variable and at the same time to set the value of a variable, an assignment expression is used. If a variable with the same name already exists, the value of the already existing variable is modified. If a variable with the same name does not already exist, the shell will create a new local variable and will set it's value:

testvariable=value

You can use the echo command to print the value of the new variable

testuser1@testuser1-host:~$ echo $testvariable
value

In the example above, testvariable was created as a local variable, meaning that the following search in the environment variables will result in no output:

testuser1@testuser1-host:~$ env | grep testvariable

In order to promote a local variable to become an environment variable, you have to use the export command.

After exporting testvariable, it becomes an environment variable. The search through the environment variables will find testvariable now:

testuser1@testuser1-host:~$ export testvariable
testuser1@testuser1-host:~$ env | grep testvariable
testvariable=value

The creation and exporting of a variable can be done using one single command. The following example demonstrates how to create anenvironment variable, and some different methods to print that variable:

testuser1@testuser1-host:~$ export testvariable2=value2
testuser1@testuser1-host:~$ printenv testvariable2
value2
testuser1@testuser1-host:~$ env | grep testvariable2
testvariable2=value2
testuser1@testuser1-host:~$ echo $testvariable2
value2

Other articles:

Linux Bash shell variables

Linux Bash shell variables - Local Variables

Linux Bash shell variables - Environment Variables

How to remove Linux Bash shell variables

How to make Linux Bash shell variables persistent

Environment variables in python - part 1

Environment variables in python - part 2

Environment variables in python - part 3