A Newbie’s Guide to Linux Environment Variables
While the term environment variables might sound like some kind of college science project, the fact is that you use them every day, whether you realize it or not. They’re essential to how Linux systems operate and make it easy to customize your environment, no matter what you’re working on. Let’s take a closer look at how these work, as well as some practical applications of using them in your daily life as a Linux user.
How do I set an environment variable in my terminal?
In your terminal, you can set an environment variable by using the `export` command. For example, to set the `foo` variable to `bar`, you would type `export foo=bar`. To see a list of all current environment variables and their values, you can use the `env` command.
How do I access an environment variable from my command line prompt or bash shell script?
In order to access an environment variable from your command line prompt or bash shell script, you must first export the variable. To do this, you can use the export command. For example, if you wanted to export the environment variable TEST, you would type export TEST. Next, you would type echo $TEST to print out the value of the TEST variable. It should return a blank space. Now you can set any value for the TEST variable that you want. If you type echo $TEST again, it should now return whatever value was assigned to it in the previous step (e.g., John).
How do I set an environment variable from within a bash shell script?
If you want to set an environment variable from within a bash shell script, there are a few ways you can do it. One way is to use the export command. For example, if you wanted to set the environment variable FOO equal to bar, you would use the following command:
Another way to set an environment variable from within a bash shell script is with declare. To set FOO equal to bar using declare, enter either of these two commands:
declare -x FOO=bar
export FOO=bar A third way (which may be more portable) is with unset.
How do I list all currently set environmental variables on my computer?
In order to list all of the environmental variables that are currently set on your computer, you will need to open a terminal window and type in the following command: env. This will give you a list of all of the environment variables that are currently set. If you want to see the value of a specific environment variable, you can type in echo $VARNAME, where VARNAME is the name of the variable you want to check.
How do I delete (unset) an environmental variable from my computer?
Deleting an environmental variable is also known as unsetting it. To unset an enviromental variable, you can use the unset command in your terminal. For example, if you wanted to delete the HOME environmental variable, you would type unset HOME into your terminal. Keep in mind that this will only delete the environmental variable from your current session. If you want to permanently delete an environmental variable, you will need to edit your .bashrc file.
When should I use $HOME vs. $(shell something)?
If you’re just starting out with Linux, you might be wondering what the difference is between $HOME and $(shell something). Here’s a quick rundown -Both of these variables refer to your current working directory. -$HOME refers to your home directory (the directory in which you typically store your files), while $(shell something) refers to the directory that was last accessed by the command shell (the program that provides a command line interface for navigating through your computer’s filesystem) before it issued that command.