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

参考链接:

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 Variable Meaning To View Variable Value Type
BASH_VERSION Holds the version of this instance of bash. echo $BASH_VERSION
HOSTNAME The name of the your computer. echo $HOSTNAME
CDPATH The search path for the cd command. echo $CDPATH
HISTFILE The name of the file in which command history is saved. echo $HISTFILE
HISTFILESIZE The maximum number of lines contained in the history file. echo $HISTFILESIZE
HISTSIZE The number of commands to remember in the command history. The default value is 500. echo $HISTSIZE
HOME The home directory of the current user. echo $HOME
IFS The 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
LANG Used to determine the locale category for any category not specifically selected with a variable starting with LC_. echo $LANG
PATH The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. echo $PATH
PS1 Your prompt settings. echo $PS1
TMOUT The 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
TERM Your login terminal type. echo $TERM
export TERM=vt100
SHELL Set path to login shell. echo $SHELL
DISPLAY Set X display name echo $DISPLAY
export DISPLAY=:0.1
EDITOR Set 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

猜你喜欢

转载自darrenzhu.iteye.com/blog/2108302