In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to set environment variables in Linux system, the content is very detailed, interested friends can refer to it, I hope it can help you.
environment variable
Environment variables are implemented as key-value pairs, are system-wide variables, and are inherited by all derived child processes and shells. Environment variable names are case-sensitive and are usually named in upper case (MYVAR1, MYVAR2... )
A single-valued environment variable looks something like this:
KEY=value1
If you want to assign more than one value to an environment variable, you usually use a colon ( : ) as the delimiter. Each key-value pair ends up looking like this:
KEY=value1:value2:value3
If the value you want to assign to an environment variable contains spaces, you need to use quotation marks:
KEY="value with spaces"Shell variable
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 use the same rules as environment variables.
If you want Shell variables to be used as global variables, you can use the export directive:
$ export MYVAR=lxlinux.net$ echo $MYVARlxlinux.net$ env |grep MYVARMYVAR= lxlinux.net Common environment variables and Shell variables
Some environment variables and Shell variables are useful and often referenced. Here are some common environment variables that you may encounter later:
Variable name meaning $TERM This specifies the type of terminal to emulate when running Shell. Different hardware terminals can be simulated for different operational requirements. But you don't usually need to worry about this variable. USER Currently logged in user PWD Current working directory OLDPWD A working directory on which this variable is saved by Shell to switch back to the previous working directory by executing cd -. LS_COLORS This defines the color output code for the ls instruction, which is used to add color output to the ls instruction. This is often used to distinguish between different file types and to make information such as file types clear to the user. MAIL Path to the current user mailbox PATH List of directories the system checks for instructions. When the user enters a command, the system checks the directories in the order of the directory list for the corresponding executable file. LANG Current language and localization settings, including character encoding. HOME Current user's home directory_Last command executed
In addition to the above environment variables, you may also encounter the following Shell variables frequently:
The variable name meaning BASHOPTS is a list of options enabled when bash is executed, which is helpful in determining whether the Shell environment is functioning as expected. BASH_VERSION Executing bash version in human-readable format BASH_VERSINFO Executing bash version in machine-readable format COLUMNS The number of wide columns used to set output information drawn to the screen DIRSTACKpushd and popd commands are available in the directory stack HISTFILESIZE The number of lines of command history stored in a file. The default is ~/.bash_history file number of rows. HISTSIZE The number of rows of command history allowed to be stored in memory, i.e., the number of rows printed by the histroy command. HOSTNAME Host name of the computer IFS internal field delimiter used to separate input on the command line. By default, spaces are used as separators. PS1 defines the main command prompt. This defines the appearance of the command prompt when starting a Shell session. PS2 is a command prompt for declaring commands that span multiple lines. SHELLOPTS Shell options that can be set with the set command. UID The UID of the current user (user ID) View Shell and Environment Variables
On Linux, there are several commands that allow you to view environment variables:
env -This command allows you to run programs in a custom environment without changing the current environment. When the env command is used without arguments, it prints out the current list of environment variables.
printenv -prints out all or specified environment variables.
set -This command sets or deletes Shell variables. When used without arguments, the set command prints a list of all variables, including environment variables and Shell variables, and Shell functions.
By default, env and printenv function exactly the same:
$ printenv SSH_CONNECTION=10.0.2.2 37182 10.0.2.15 22LESSCLOSE=/usr/bin/lesspipe %s %sLANG=C.UTF-8XDG_SESSION_ID=5USER=alvinMYVAR=lxlinux.netPWD=/home/alvinHOME=/home/alvinSSH_CLIENT=10.0.2.2 37182 22XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktopSSH_TTY=/dev/pts/0MAIL=/var/mail/alvinTERM=xterm-256colorSHELL=/bin/bashSHLVL=1LOGNAME=alvinXDG_RUNTIME_DIR=/run/user/1000PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/binLESSOPEN=| /usr/bin/lesspipe %s_=/usr/bin/printenv
The env command differs from the printenv command only in more specific functions. For example, using the printenv command, you can request the value of a single variable:
$ printenv SHELL/bin/bash$ printenv HOME/home/alvin$ printenv MYVARlxlinux.net
The env command modifies the environment in which a program runs by passing a set of variables to the command:
env MYVAR=lxlinux.net command_to_run command_options
Printenv and env can only print environment variables, but if you want to print a list of all variables or Shell functions, you can use the set directive.
$ setBASH=/bin/bashBASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepathBASH_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=140DIRSTACK=()EUID=1000GROUPS=()HISTCONTROL=ignorebothHISTFILE=/home/alvin/.bash_historyHISTFILESIZE=2000HISTSIZE=1000HOME=/home/alvinHOSTNAME=ubuntu-bionicHOSTTYPE=x86_64IFS=$' \t\n'LANG=C.UTF-8LESSCLOSE='/usr/bin/lesspipe %s %s'LESSOPEN='| /usr/bin/lesspipe %s'LINES=35LOGNAME=alvin.....
This command will display a large list of all the variables, so you might want to pass the output to the less command.
$ set |less Setting Shell Variables and Environment Variables
Linux environment variables can be set in the following commands:
set -This command sets or unsets Shell variables. When used without arguments, the set command prints a list of all variables, including environment variables and Shell variables, and Shell functions.
unset -This command removes Shell variables as well as environment variables.
export -This command sets environment variables.
To better understand the difference between Shell variables and environment variables, let's start with setting Shell variables and move on to environment variables.
Start by defining a Shell variable in the current session:
$ MYVAR=lxlinux
You can verify that the variable is set with echo $MYVAR:
$ echo $MYVARlxlinux
Verify that the variable is an environment variable using the printenv command:
$ printenv MYVAR
No output returns, which means MYVAR variables are not environment variables.
The export command can be used to set environment variables. To create an environment variable, simply export the Shell variable as an environment variable with the export command:
$ export MYVAR
You can test it with the following statement:
$ printenv MYVARlxlinux
Of course, you can also set environment variables with just one line of code:
$ export MYNEWVAR="My New Variable"
But environment variables created in this way can only be used in the current session, and if you open a new Shell session or log out, all variables will be lost.
We can also restore environment variables to Shell variables or delete them altogether:
MYVAR variables are defined as environment variables and we can revert them to Shell variables by typing:
$ export -n MYVAR
MYVAR variables are no longer environment variables, but they are still Shell variables.
Whether it is a Shell variable or an environment variable, if you want to delete the variable completely, you can use the unset command to delete:
$ unset MYVAR
You can verify that the MYVAR variable has been deleted by:
$ echo $MYVAR
Since the variable has been deleted, nothing is returned.
Persistence of environmental variables
Many programs use environment variables to determine how they execute, but we don't want to have to reset important variables every time we start a new Shell session, so we need to write important environment variables to the configuration file.
Shell sessions start differently, for example, interactive shells connected to the terminal and non-interactive shells not connected to the terminal, login shells and non-login shells, and bash shells read different configuration files depending on how the session starts.
However, in most Linux distributions, when you start a new Shell session, environment variables are typically read from the following files:
/etc/environment -Use this file to set system-wide available environment variables.
/etc/profile -Whenever bash logs into the Shell, the variables set in this file are loaded.
~/.bashrc -Each user-specific Shell profile. For example, if you are using Bash, you can declare variables in it.
To load new environment variables into the current Shell session, use the source command:
$ source ~/.bashrc
If you want to set environment variables, consider adding them to/etc/profile,/etc/bash.bashrc, or/etc/environment files.
conclusion
In this article, we learned about some common environment variables and Shell variables, and how to set and view these variables. In fact, these variables have always been in our Shell session, and they are useful for many programs. There are many common scenarios where these variables are referenced. I hope this helps you with 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 a Linux learning roadmap, so I used my spare time to stay up for a month and organized an e-book based on my own experience. Whether you are interviewing or self-improvement, I believe it will help you! The table of contents reads as follows:
About how to set environment variables in Linux system to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.