Hello everyone, this is Liang Xu.
Everyone knows that in Linux systems, there are two types of variables: environment variables and shell variables.
Environment variables are globally available in the program and its subprograms, and are often used to store information such as the default text editor or browser, the path of the executable file, and so on. Shell variables are only available in the current Shell and can be used to store information such as the ID of the current user.
So what are environment variables, what are Shell variables, and how to set and view these two variables?
Next, let Liang Xu answer for you:
Environment variable
Environment variables are implemented in the form of key-value pairs. They are variables that are available throughout the system and are inherited by all derived child processes and shells. The names of environment variables are case-sensitive and are usually named in uppercase (MYVAR1, MYVAR2...)
An environment variable with a single value looks like this:
KEY=value1
If you want to assign multiple values to an environment variable, you usually use a colon (:) as the separator. Each key-value pair finally looks like this:
KEY=value1:value2:value3
If the value to be assigned to the environment variable contains spaces, you need to use quotation marks:
KEY="value with spaces"
Shell variables
Shell variables are variables in the Shell that are specifically used to set or define them. Each Shell, such as zsh and bash, has its own set of internal Shell variables. They are usually used to track temporary data, such as the current working directory, and their usage is the same as environment variables.
If you want to use Shell variables as global variables, you can use the export
command:
$ export MYVAR=lxlinux.net
$ echo $MYVAR
lxlinux.net
$ env | grep MYVAR
MYVAR=lxlinux.net
Common environment variables and shell variables
Some environment variables and shell variables are very useful and are often quoted. The following are some common environment variables that you may encounter in the future:
variable name | meaning |
---|---|
TERM | This specifies the type of terminal to be simulated when running the Shell. It can simulate different hardware terminals for different operation requirements. However, you usually don't need to worry about this variable. |
USER | Currently logged in user |
PWD | Current working directory |
OLDPWD | On a working directory, this variable is kept by Shell, so that by performing cd - back to the previous working directory. |
LS_COLORS | This defines the ls color output code instructions for the ls added color output instruction. This is usually used to distinguish different file types and make the user know the file type and other information at a glance. |
Path of current user mailbox | |
PATH | The list of directories that the system will check when looking for instructions. When the user enters an instruction, the system will check the directories in the order of this directory list to find the corresponding executable files. |
LANG | Current language and localization settings, including character encoding. |
HOME | Home directory of current user |
_ | The last command executed |
In addition to the above environment variables, you may also often encounter the following shell variables:
variable name | meaning |
---|---|
BASHOPTS | The list of options that are enabled when bash is executed, which is helpful for determining whether the shell environment is running as expected. |
BASH_VERSION | The running bash version in human readable format |
BASH_VERSINFO | The running bash version in machine-readable format |
COLUMNS | Used to set the number of wide columns of output information drawn on the screen |
DIRSTACK | pushd And popd command the directory stack available. |
HISTFILESIZE | The number of lines of the command history stored in the file. The default is ~/.bash_history the number of lines in the file. |
HISTSIZE | Memory allows the storage of the number of lines of command history, that histroy command the number of lines that can be printed out. |
HOSTNAME | Hostname of the computer |
IFS | Internal field separator, used to separate input on the command line. By default, a space is used as the separator. |
PS1 | Define the main command prompt. This is used to define the appearance of the command prompt when starting a shell session. PS2 is used to declare a command prompt that spans multiple lines. |
SHELLOPTS | You can use set the command to set the Shell option. |
UID | UID (User ID) of the current user |
View shell variables and environment variables
In Linux systems, there are several commands that allow you to view environment variables:
env
— This command allows you to run the program in a customized environment without changing the current environment. When used without parameters env
command, it will print out the current list of environment variables.
printenv
— Can print out all or specified environment variables.
set
— This command can set or delete Shell variables. When used without parameters set
command, it will print out a list of all variables, including environment variables and variables, including Shell and the Shell function.
By default, env
and the printenv
function is exactly the same:
$ printenv
SSH_CONNECTION=10.0.2.2 37182 10.0.2.15 22
LESSCLOSE=/usr/bin/lesspipe %s %s
LANG=C.UTF-8
XDG_SESSION_ID=5
USER=alvin
MYVAR=lxlinux.net
PWD=/home/alvin
HOME=/home/alvin
SSH_CLIENT=10.0.2.2 37182 22
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
SSH_TTY=/dev/pts/0
MAIL=/var/mail/alvin
TERM=xterm-256color
SHELL=/bin/bash
SHLVL=1
LOGNAME=alvin
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
LESSOPEN=| /usr/bin/lesspipe %s
_=/usr/bin/printenv
Only in the more specific features to reflect the env
command and printenv
command difference. For example, using the printenv
command, you can request the value of a single variable:
$ printenv SHELL
/bin/bash
$ printenv HOME
/home/alvin
$ printenv MYVAR
lxlinux.net
env
The command can modify the environment in which the program runs by passing a set of variables to the command:
env MYVAR=lxlinux.net command_to_run command_options
printenv
And env
command can only print out the environment variable, and if you want to print out a list of all the variables or the Shell function, you can use the set
command.
$ set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_VERSINFO=([0]="2" [1]="8")
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="4" [2]="20" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
BASH_VERSION='4.4.20(1)-release'
COLUMNS=140
DIRSTACK=()
EUID=1000
GROUPS=()
HISTCONTROL=ignoreboth
HISTFILE=/home/alvin/.bash_history
HISTFILESIZE=2000
HISTSIZE=1000
HOME=/home/alvin
HOSTNAME=ubuntu-bionic
HOSTTYPE=x86_64
IFS=$' \t\n'
LANG=C.UTF-8
LESSCLOSE='/usr/bin/lesspipe %s %s'
LESSOPEN='| /usr/bin/lesspipe %s'
LINES=35
LOGNAME=alvin
.....
This command will display a large list of all the variables, so you might want to pass to the output of less
the command.
$ set | less
Set shell variables and environment variables
There are several commands that can set environment variables in Linux system:
set
— This command can set or unset Shell variables. When used without parameters set
command, it will print out a list of all variables, including environment variables and variables, including Shell and the Shell function.
unset
— This command can delete shell variables and environment variables.
export
-This command can set environment variables.
In order to better understand the difference between Shell variables and environment variables, let's start with setting Shell variables, and then talk about environment variables.
Start by defining a Shell variable in the current session:
$ MYVAR=lxlinux
You can use echo $MYVAR
to verify that the variable is set:
$ echo $MYVAR
lxlinux
Use printenv
command to verify that the variable is an environment variable:
$ printenv MYVAR
No output is returned, which means that the MYVAR variable is not an environment variable.
export
Commands can be used to set environment variables. To create an environment variable, just use export
the Shell command variable exported as environment variables:
$ export MYVAR
You can check with the following statement:
$ printenv MYVAR
lxlinux
Of course, you can also set environment variables with just one line of code:
$ export MYNEWVAR="My New Variable"
But the environment variables created in this way can only be used in the current session. If you open a new Shell session or log out, all the variables will be lost.
We can also restore environment variables to Shell variables, or delete them completely:
The MYVAR variable is defined as an environment variable, and we can restore it to a Shell variable by entering the following code:
$ export -n MYVAR
In this way, the MYVAR variable is no longer an environment variable, but still a Shell variable.
Whether Shell variables or environmental variables, you want to completely remove the variable, you can use the unset
command to delete:
$ unset MYVAR
The following statement can be used to verify that the MYVAR variable has been deleted:
$ echo $MYVAR
Since the variable has been deleted, nothing is returned.
Persistence of environment variables
Many programs need to use environment variables to determine the specific execution method, but we do not want to reset important variables every time a new Shell session is started, so we need to write important environment variables into the configuration file.
Shell sessions can be started in different ways, for example, interactive shells connected to the terminal and non-interactive shells not connected to the terminal, as well as login shells and non-login shells. The bash shell varies according to the different startup methods of the session. Read different configuration files.
However, in most Linux distributions, when you start a new Shell session, you generally read environment variables from the following files:
/etc/environment
— Use this file to set system-wide environment variables available.
/etc/profile
— Whenever bash logs in to Shell, the variables set in this file will be loaded.
~/.bashrc
— Shell configuration files specific to each user. For example, if you are using Bash, you can declare variables in it.
If you want to load new environment variables into the current Shell session, you can use the source command:
$ source ~/.bashrc
If you want to set environment variables, you can consider adding it to /etc/profile
, /etc/bash.bashrc
or /etc/environment
file.
in conclusion
In this article, we learned about some common environment variables and Shell variables, and learned how to set and view these variables. In fact, these variables have always been in our Shell session and are useful for many programs. There are many common All scenes will reference these variables. Hope this will help your work too. If you still have any questions about these two variables, please leave a message and let me know!
Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:
Give it to everyone for free, just ask you to give me a thumbs up!
Ebook | Linux development learning roadmap
I also hope that some friends can join me to make this e-book more perfect!
Gain? I hope the old irons will have a three-strike combo so that more people can read this article
Recommended reading: