This post is all about Environemt Varibles in Linux as they are really important to learn and understand for a Linux User.
Environment Variables
Environment Variables are variables that are available to all programs in an evnironemnt. These variables define how the porcess should run depending on their value.
Environment Variables are strings of key value pairs and are stored like this KEY=value or KEY=val1:val2. If there are more values to one KEY they are separated by a colon.
These varibales are used to hide some values that you dont want anyone to have access to. For Example: It could be an API Key or an access token etc.
Viewing Environment Varibales in Linux
To view all the environment variables in Linux just type env
in the command line.
root@User:~$ env
PATH=/user/local/bin
PS1=\u@\h:\w$
...
...
This command will display all the variables that are available to the environment. Here are shown only two vars, one is PATH and the other is PS1. These two are the only ones that I will talk about here.
PATH
PATH is an environment variables that points to the path for all the commands that you type in the shell such as ls or cd etc.
root@User:~$ echo $PATH
/usr/local/sbin:/usr/local/bin
These are directories that the bash will look in when you type a command and if the binary for the command is not found in PATH variables it will return command not found.
Change PATH Variable
If you install a new software or compile the binaries youself you won't be able to use that in the terminal becasue the terminal looks for the binaries to run in the directories that are defined in the PATH variable.
To edit a variable you do it like this KEY=value
in the shell but to add the directory to PATH variable you have to do this the following way.
root@User:~$ echo $PATH
/usr/local/bin:/usr/local/sbin
root@User:~$ PATH=$PATH:/root/new-dir
root@User:~$ echo $PATH
/usr/local/bin:/usr/local/sbin:/root/new-dir
The reason behid doing it this way is because if you edit the PATH by KEY=value
method it will overwrite the PATH variable with only the value you provide and you might not be able to run commands like ls
or cd
etc.
PS1 Environment Variable
PS1 environment variable is a variable that defines how your prompt should look like. For example in the upper example it looks like this root@User:~$
and that is becaue it is stored as a var like this \h@\u:\w$
.
root@User:~$ echo $PS1
"\h@\u:\w$"
And it can be changed the same way we did with PATH but a little different.
root@User:~$ PS1="Linux>"
Linux> echo $PS1
"Linux>"
Linux> PS1="\u:\w >>"
User:~ >> cd tmp
User:/tmp >>
The PS1 variable have some placeholders that can be used to add system information into the prompt. Some of them are following.
Placeholder | Meaning |
---|---|
\u | For displaying User |
\w | For displaying Current Directory |
\h | For displaying Host |
\t | For displaying Time |
User:/tmp >> PS1="Linux:\w >>"
Linux:/tmp >> cd temp
Linux:/tmp/temp >> cd ~
Linux:~ >> pwd
/home/User
Creating Your Own Environment Variables
To create your own variables you can do so by the same way you edit a variable. When editing a variable in the terminal if it already exists it will be overwritten but if it does not a new one will be created.
Linux:~ >> echo $NEWVAR
Linux:~ >> NEWVAR="This is a new Variable!"
Linux:~ >> echo $NEWVAR
"This is a new Variable!"