๐ŸŒฑ 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 environ…


This content originally appeared on DEV Community and was authored by Prashant Pal

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


This content originally appeared on DEV Community and was authored by Prashant Pal


Print Share Comment Cite Upload Translate Updates
APA

Prashant Pal | Sciencx (2024-10-28T12:25:43+00:00) ๐ŸŒฑ How to Set Environment Variables in Linux. Retrieved from https://www.scien.cx/2024/10/28/%f0%9f%8c%b1-how-to-set-environment-variables-in-linux/

MLA
" » ๐ŸŒฑ How to Set Environment Variables in Linux." Prashant Pal | Sciencx - Monday October 28, 2024, https://www.scien.cx/2024/10/28/%f0%9f%8c%b1-how-to-set-environment-variables-in-linux/
HARVARD
Prashant Pal | Sciencx Monday October 28, 2024 » ๐ŸŒฑ How to Set Environment Variables in Linux., viewed ,<https://www.scien.cx/2024/10/28/%f0%9f%8c%b1-how-to-set-environment-variables-in-linux/>
VANCOUVER
Prashant Pal | Sciencx - » ๐ŸŒฑ How to Set Environment Variables in Linux. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/28/%f0%9f%8c%b1-how-to-set-environment-variables-in-linux/
CHICAGO
" » ๐ŸŒฑ How to Set Environment Variables in Linux." Prashant Pal | Sciencx - Accessed . https://www.scien.cx/2024/10/28/%f0%9f%8c%b1-how-to-set-environment-variables-in-linux/
IEEE
" » ๐ŸŒฑ How to Set Environment Variables in Linux." Prashant Pal | Sciencx [Online]. Available: https://www.scien.cx/2024/10/28/%f0%9f%8c%b1-how-to-set-environment-variables-in-linux/. [Accessed: ]
rf:citation
» ๐ŸŒฑ How to Set Environment Variables in Linux | Prashant Pal | Sciencx | https://www.scien.cx/2024/10/28/%f0%9f%8c%b1-how-to-set-environment-variables-in-linux/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.