环境变量的介绍与使用

环境变量的介绍与使用

什么是环境变量
  当系统在运行的时候,因为工作的需求,需要用到许多的信息,比如:但前登录的用户是谁?用户输入的命令去那里找对应的程序?用户的命令历史记录最大是多少?当前用户使用的SHELL是什么?等等等等。那么计算机需要这些信息的时候又去那里取得呢?这就是环境变量的根本作用了。他提供给计算机常用的信息。

我们经常能见到的,熟悉的环境变量
  PATH:可执行文件(命令)的存放目录
  HOME:用户家目录
  HISTSIZE是指保存历史命令记录的条数。
  LOGNAME是指当前用户的登录名。
  HOSTNAME是指主机的名称,许多应用程序如果要用到主机名的话,通常是从这个环境变量中来取得的。
  SHELL是指当前用户用的是哪种Shell。
  LANG/LANGUGE是和语言相关的环境变量,使用多种语言的用户可以修改此环境变量。
  MAIL是指当前用户的邮件存放目录。

显示环境变量
[code:1]echo $HOME[/code:1]

设置一个新的环境变量
设置一个新的环境变量WELCOME
[code:1]export WELCOME="Hello"[/code:1]

重写环境变量
重写环境变量WELCOME(调用变量时给变量加$符)
[code:1]export $WELCOME="Hello"[/code:1]

在原来的环境变量上追加内容
使WELCOME的值变为Hello:Hi
[code:1]export WELCOME=$WELCOME:Hi[/code:1]

显示所有的环境变量
[code:1]$ env[/code:1]

显示所有本地定义的Shell变量
[code:1]$ set[/code:1]

清除环境变量
清除环境变量WELCOME
[code:1]$ unset $WELCOME[/code:1]


如果觉得每次修改环境变量太麻烦的话,建议把环境变量的设置写在一个脚本里,通常写在/etc/profile或者是~/.bashrc中    因为这两个文件在启动是都会运行
/etc/profile   对于每个用户都生效,启动时执行
~/.bashrc  对于当前登录用户生效,登录后执行
当两个文件的设置出现冲突时,以~/.bashrc为准
如果修改了  ~/.bashrc 需要重新启动才能生效。

但是你可以 source ~/.bashrc 一下~

嘻嘻

楼主忘了说
不错 不错  
要是列出所有的系统变量就好了
\a The ASCII bell character (you can also type \007)
\d Date in "Wed Sep 06" format
\e ASCII escape character (you can also type \033)
\h First part of hostname (such as "mybox")
\H Full hostname (such as "mybox.mydomain.com")
\j The number of processes you've suspended in this shell by hitting ^Z
\l The name of the shell's terminal device (such as "ttyp4")
\n Newline
\r Carriage return
\s The name of the shell executable (such as "bash")
\t Time in 24-hour format (such as "23:01:01")
\T Time in 12-hour format (such as "11:01:01")
\@ Time in 12-hour format with am/pm
\u Your username
\v Version of bash (such as 2.04)
\V Bash version, including patchlevel
\w Current working directory (such as "/home/drobbins")
\W The "basename" of the current working directory (such as "drobbins")  
\! Current command's position in the history buffer
\# Command number (this will count up at each prompt, as long as you type something)
\$ If you are not root, inserts a "$"; if you are root, you get a "#"
\xxx Inserts an ASCII character based on three-digit number xxx (replace unused digits with zeros, such as "\007")
\\ A backslash
\[ This sequence should appear before a sequence of characters that don't move the cursor (like color escape sequences). This allows bash to calculate word wrapping correctly.
\] This sequence should appear after a sequence of non-printing characters.