Linux环境(Environment)设置相关命令

本文深入探讨了Linux系统环境变量的基本概念、如何查看和设置它们,以及如何将这些设置永久保存。包括如何使用`export`命令修改环境变量,如`PATH`、`JAVA_HOME`等,并介绍了如何在`.bash_profile`文件中进行持久化设置。同时提供了常见环境变量的解释,如`BASH_VERSION`、`HOSTNAME`、`PATH`、`PS1`等,并演示了如何显示和使用单个环境变量的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考链接:

http://www.cyberciti.biz/faq/set-environment-variable-linux/

http://bash.cyberciti.biz/guide/Variables#Commonly_Used_Shell_Variables

 

列出所有系统环境变量

To see all system variables, type the following command at a console / terminal:

set

OR

env

OR

printenv

 

给环境变量赋值

You can modify each environmental or system variable using the export command. Set the PATH environment variable to include the directory where you installed the bin directory with perl and shell scripts:

 
export PATH=${PATH}:/home/vivek/bin
 

OR

 
export PATH=${PATH}:${HOME}/bin
 

To set the JAVA_HOME environment variable to the directory where you installed the J2SE SDK application, enter:

 
export PATH=${PATH}:/usr/java/jdk1.5.0_07/bin
 

You can set multiple paths as follows:

 
export ANT_HOME=/path/to/ant/dir
export PATH=${PATH}:${ANT_HOME}/bin:${JAVA_HOME}/bin
 

 

How Do I Make All Settings permanent?

The ~/.bash_profile ($HOME/.bash_profile) or ~/.prfile file is executed when you login using console or remotely using ssh. Type the following command to edit ~/.bash_profile file, enter:
$ vi ~/.bash_proflle
Append the $PATH settings, enter:
export PATH=${PATH}:${HOME}/bin
Save and close the file.

 

A Note About /etc/profile File

/etc/profile contains Linux system wide environment and startup programs. It is used by all users with bash, ksh, sh shell. Usually used to set PATH variable, user limits, and other settings for user. It only runs for login shell. If you wanted to make large changes or application specific changes use /etc/profile.d/ directory as explained here andhere.

 

 

Commonly Used Shell Variables

The following variables are set by the shell:

System VariableMeaningTo View Variable Value Type
BASH_VERSIONHolds the version of this instance of bash.echo $BASH_VERSION
HOSTNAMEThe name of the your computer.echo $HOSTNAME
CDPATHThe search path for the cd command.echo $CDPATH
HISTFILEThe name of the file in which command history is saved.echo $HISTFILE
HISTFILESIZEThe maximum number of lines contained in the history file.echo $HISTFILESIZE
HISTSIZEThe number of commands to remember in the command history. The default value is 500.echo $HISTSIZE
HOMEThe home directory of the current user.echo $HOME
IFSThe Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>.echo $IFS
LANGUsed to determine the locale category for any category not specifically selected with a variable starting with LC_.echo $LANG
PATHThe search path for commands. It is a colon-separated list of directories in which the shell looks for commands.echo $PATH
PS1Your prompt settings.echo $PS1
TMOUTThe default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the command. If not input provided it will logout user.echo $TMOUT
TERMYour login terminal type.echo $TERM
export TERM=vt100
SHELLSet path to login shell.echo $SHELL
DISPLAYSet X display nameecho $DISPLAY
export DISPLAY=:0.1
EDITORSet name of default text editor.export EDITOR=/usr/bin/vim
  • Note you may add above variable (export command) to the initialization file located in the home directory of your account such as ~/.bash_profile.

 

显示单个环境变量的值

Use echo command to display variable value. To display the program search path, type:

echo "$PATH"

To display your prompt setting, type:

echo "$PS1"

All variable names must be prefixed with $ symbol, and the entire construct should be enclosed in quotes. Try the following example to display the value of a variable without using $ prefix:

echo "HOME"

To display the value of a variable with echo $HOME:

echo "$HOME"

You must use $ followed by variable name to print a variable's contents.

The variable name may also be enclosed in braces:

echo "${HOME}"

This is useful when the variable name is followed by a character that could be part of a variable name:

echo "${HOME}work"
Say hello to printf

The printf command is just like echo command and is available under various versions of UNIX operating systems. It is a good idea to use printf if portability is a major concern for you. The syntax is as follows:

printf "$VARIABLE_NAME\n"
printf "String %s" $VARIABLE_NAME
printf "Signed Decimal Number %d" $VARIABLE_NAME
printf "Floating Point Number %f" $VARIABLE_NAME

To display the program search path, type:

printf "$PATH\n"

OR

printf "The path is set to %s\n" $PATH
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值