๐ 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. ๐ค
Top comments (0)