π Each user has their own system configuration, like which π₯οΈ browser is set as default, which π§ shell is set as default, etc. π€ Users can configure their system environment according to their needs. Environment Variables are used to store user environment information.
1.π The information in environment variables is represented in form of KEY = VALUE pairs, for eg. SHELL=/bin/bash
.
Here SHELL is the name of the variable and /bin/bash is the value.
- The environment variables are available for whole environment π.
- Remember name of all variables should be uppercase π .
- You can change environment variables values accordingly π.
How to see env in CLI π
To print env variables there is command printenv
that prints all env variables SHELL, HOME, USERNAME, LANG etc π¨οΈβ¨.
You can print specific env variable by typing the variable name, for eg. printenv USER
π¨οΈ. You can also use pipes and grep command to find similar variable name ππ οΈ.
π± Creating Own Environment Variables
πWhy to create own environment variables,
πTo protect sensitive data for an application. When the application needs to run on servers it needs credentials to access. You can use environment variables to store those user name and password.
By this application becomes more flexible π.
Now lets create:-
-
export
is used to create a env variable, for eg:-
2.π« To remove the variable we use unset
.
NOTE:- π Using export command to make variable is only available in current session. Once the CLI is closed all variables are gone.β
π How to set env permanently
There is a file called shell specific configuration file. It is set according to user default shell. For eg. if you are using BASH, you can declare the variables in the ~/.bashrc
file.
π Open the file with vim, vim .bashrc
and then type the variables you want to add. Syntax for adding the variable is same as we have discussed above. And after adding π the variables to .bashrc file you need to refresh it by typing the command source .bashrc
.
πResources
- https://www.freecodecamp.org/news/how-to-set-an-environment-variable-in-linux/
- https://docs.digitalocean.com/glossary/environment-variable/
Conclusion
Setting environment variables in Linux is a powerful skill that can significantly enhance your development and system administration tasks. π» We discussed how to create, modify, and remove these variables π.
If you have any questions or tips to share about working with environment variables, please leave a comment below. π¬ Thank you for reading, and happy coding! π
You can check this on hasnode too.π₯° https://prashantsdevlog.hashnode.dev/
In this week I'll learn networking skills in linux essential for a DEVOPS engineer. π€