Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the env command in Linux

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you how to use the env command in Linux, I believe most people still do not know how to use it, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

The env command in Linux displays the environment variables of the current user and can be used to execute other commands under specified environment variables. Here's a comparison of the set, env, and export commands: the set command displays the variables of the current shell, including the variables of the current user; the env command displays the variables of the current user; and the export command displays the shell variables currently exported as user variables. Each shell has its own unique variable (set) displayed variables, this and user variables are different, the current user variables and what shell you use has nothing to do with, no matter what shell you use, such as HOME, SHELL and other variables, but the shell's own variables are different, such as BASH_ARGC, BASH and so on, these variables will only be displayed set, bash is unique, export without parameters, show which variables are exported as user variables, Because a shell's own variable can be turned into a user variable by exporting.

common way

Format: env

Print the environment variables for the current user.

Env -i NAME1=VALUE1 NAME2=VALUE2

Executes the command line using the specified environment variables.

uses examples

Example 1: Compare env,set and export

The set command prints the most information, including custom functions, so much so that you have to use more to see it.

[root@www.linuxidc.com ~]# envHOSTNAME=webTERM=linuxSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=58.222.185.248 1113 22SSH_TTY=/dev/pts/6ANT_HOME=/opt/apache/apache-ant-1.8.1USER=rootLS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:SSH_AUTH_SOCK=/tmp/ssh-amfuW25778/agent.25778MAIL=/var/spool/mail/rootPATH=/usr/kerberos/sbin:/usr/kerberos/bin:/opt/apache/apache-ant-1.8.1/bin:/usr/java/jdk1.6.0_20/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binINPUTRC=/etc/inputrcPWD=/rootJAVA_HOME=/usr/java/jdk1.6.0_20LANG=zh_CN.GB18030SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpassSHLVL=1HOME=/rootLOGNAME=rootCVS_RSH=sshSSH_CONNECTION=58.222.185.248 1113 218.23.142.44 22LESSOPEN=|/usr/bin/lesspipe.sh %sG_BROKEN_FILENAMES=1_=/bin/env[root@www.linuxidc.com ~]# exportdeclare -x ANT_HOME="/opt/apache/apache-ant-1.8.1"declare -x CVS_RSH="ssh"declare -x G_BROKEN_FILENAMES="1"declare -x HISTSIZE="1000"declare -x HOME="/root"declare -x HOSTNAME="web"declare -x INPUTRC="/etc/inputrc"declare -x JAVA_HOME="/usr/java/jdk1.6.0_20"declare -x LANG="zh_CN.GB18030"declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s"declare -x LOGNAME="root"declare -x LS_COLORS="no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:"declare -x MAIL="/var/spool/mail/root"declare -x OLDPWDdeclare -x PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/opt/apache/apache-ant-1.8.1/bin:/usr/java/jdk1.6.0_20/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"declare -x PWD="/root"declare -x SHELL="/bin/bash"declare -x SHLVL="1"declare -x SSH_ASKPASS="/usr/libexec/openssh/gnome-ssh-askpass"declare -x SSH_AUTH_SOCK="/tmp/ssh-amfuW25778/agent.25778"declare -x SSH_CLIENT="58.222.185.248 1113 22"declare -x SSH_CONNECTION="58.222.185.248 1113 218.23.142.44 22"declare -x SSH_TTY="/dev/pts/6"declare -x TERM="linux"declare -x USER="root"[root@www.linuxidc.com ~]# set | moreANT_HOME=/opt/apache/apache-ant-1.8.1BASH=/bin/bashBASH_ARGC=()BASH_ARGV=()BASH_COMPLETION=/etc/bash_completionBASH_COMPLETION_DIR=/etc/bash_completion.dBASH_LINENO=()BASH_SOURCE=()BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-RedHat-linux-gnu")BASH_VERSION='3.2.25(1)-release'COLORS=/etc/DIR_COLORSCOLUMNS=132CVS_RSH=sshDIRSTACK=()EUID=0GROUPS=()G_BROKEN_FILENAMES=1HISTFILE=/root/.bash_historyHISTFILESIZE=1000HISTSIZE=1000HOME=/rootHOSTNAME=webHOSTTYPE=i686IFS=$' \t\n'INPUTRC=/etc/inputrcJAVA_HOME=/usr/java/jdk1.6.0_20LANG=zh_CN.GB18030LESSOPEN='|/usr/bin/lesspipe.sh %s'LINES=41LOGNAME=rootLS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:'MACHTYPE=i686-RedHat-linux-gnuMAIL=/var/spool/mail/rootMAILCHECK=60OPTERR=1OPTIND=1OSTYPE=linux-gnu--More--

The env command is used in the source code of the service command.

It can be seen that service is executing other command expressions, and there are only three environment variables: LANG, PATH, and TERM.

[root root@www.linuxidc.com ~]# grep env /sbin/serviceenv -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" statusenv -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" stopenv -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" startenv -i LANG="$LANG" PATH="$PATH" TERM="$TERM""${SERVICEDIR}/${SERVICE}" ${OPTIONS}[root@www.linuxidc.com ~]#Above is "How to use env command in Linux" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report