🌱 How to Set Environment Variables in Linux

PrashantPrashant
2 min read

🌐 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 Dev Community too https://dev.to/prashant_1111πŸ₯°

In this week I'll learn networking skills in linux essential for a DEVOPS engineer. πŸ€—

1
Subscribe to my newsletter

Read articles from Prashant directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prashant
Prashant