If you've been using Linux for a long time, you may be wondering how to set certain parameters from the command line that can keep your settings across programs. Environment variables are how you do this.
By the end, you will have a deep understanding of what environment variables are and how you can create such variables from the command line.
What are environment variables?
Environment variables are variables that should be consistent across all instances of the Linux shell. When you start a program from the shell, it makes a copy of itself, or "forks", and then replaces itself with the program that will run it, known as "exec".
Environment variables are inherited in all copies of the shell that the shell creates, also known as "exporting" variables. Even Windows uses environment variables dating back to the days of MS-DOS, but today most programs rely on the registry and their own preference menus for configuration.
An environment variable can define your preferred editor. So when a program starts the text editor to change a configuration file, this editor is displayed every time.
MAKEUSEO VIDEO OF THE DAY
Environment variables can be set system-wide by the system administrator, but it is more common for them to be set in startup files for individual users. Conventionally, they are printed in capital letters and identified by a leading "$" character, such as "$ EDITOR" for the standard text editor.
List environment variables
To view the value of an environment variable, use the echo Command. For example, to display the value of the variable $ EDITOR, run the following command:
echo $ EDITOR
If it is set, the shell will print the value, e.g. B. "vim", but if it's blank it just prints a blank line.
To display all currently set environment variables, enter "to adjust"on the command line.
Set environment variables on Linux
You can set environment variables in two ways: from the command line or in shell configuration files.
The first method is easy. In bash use the "export"Command. For example, to use the $ EDITOR Environment variable:
export EDITOR = & # 39; vim & # 39;
Note that in this syntax you use the leading "$". You can also use VARIABLE = & # 39; Value & # 39;, but that only extends to that particular instance of the shell, which means that the shell will reset the changed value as soon as you close the terminal.
Now your editor is Vim or any other text editor that you use in this session and any subshell that launches it. If you want to keep your environment variables between different shell sessions, set them in your shell's startup files.
Bash reads multiple files at startup: / etc / profile, / etc / bash / bashrc, .bash_profile, and .bashrc. The first two are system-wide and only a system administrator can change them while the rest are in your home directory.
the .bashrc File is what you want to edit as it affects interactive shells whether or not it is running as a login shell. If you are using the shell of a terminal emulator, .bashrc is read.
The method of setting environment variables in a file is the same as setting it on the command line. Just attach it export VARIABLE = & # 39; Value & # 39; Command in the shell configuration file. When you're done, save the file and start a new shell.
Note that Linux users can also change the default shell from Bash to another shell. For those who have this, you'll have to edit your shell's configuration file instead of .bashrc, this is the configuration file for bash.
Now you know how environment variables work
Environment variables allow you to use consistent settings for all of your shell sessions. Shell variables are an important part of shell scripting, something every Linux user should know.
What is shell scripting and why should you use it?
In addition to accepting and executing commands interactively, the shell can also execute commands stored in a file. This is known as shell scripting. Here we cover the basics of shell scripting.
About the author
(64 published articles)
David is a freelance writer living in the Pacific Northwest but originally from the Bay Area. He has been passionate about technology since childhood. David's interests include reading, watching quality TV shows and movies, retro games, and collecting records.
By David Delony
Subscribe to our newsletter
Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive deals!
Click here to subscribe