DEV Community

Cover image for ๐ŸŒฑ How to Set Environment Variables in Linux
Prashant Pal
Prashant Pal

Posted on

๐ŸŒฑ How to Set Environment Variables in Linux

๐ŸŒ 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.

  1. The environment variables are available for whole environment ๐ŸŒ.
  2. Remember name of all variables should be uppercase ๐Ÿ” .
  3. 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 ๐Ÿ–จ๏ธโœจ.

Printing ENV

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,

  1. ๐Ÿ”’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.

  2. By this application becomes more flexible ๐Ÿ”„.

Now lets create:-

  1. export is used to create a env variable, for eg:-

Creating env

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.

Add env

๐Ÿ“šResources

  1. https://www.freecodecamp.org/news/how-to-set-an-environment-variable-in-linux/
  2. 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)