<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>简单生活@NET &#187; 终端</title>
	<atom:link href="http://lee.kometo.com/archives/tag/%e7%bb%88%e7%ab%af/feed" rel="self" type="application/rss+xml" />
	<link>http://lee.kometo.com</link>
	<description>正确的判断来自经验，但经验往往来自错误的判断</description>
	<lastBuildDate>Mon, 06 Feb 2012 02:26:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Linux入门-终端环境(bash)设置篇</title>
		<link>http://lee.kometo.com/archives/185</link>
		<comments>http://lee.kometo.com/archives/185#comments</comments>
		<pubDate>Wed, 29 Oct 2008 17:18:31 +0000</pubDate>
		<dc:creator>Emeric lee</dc:creator>
				<category><![CDATA[IT前沿]]></category>
		<category><![CDATA[WEB应用开发]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Debian]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[终端]]></category>

		<guid isPermaLink="false">http://lee.kometo.com/?p=185</guid>
		<description><![CDATA[Bash是Debian的默认SHELL，设置终端环境（bash）使Bash更好地为我们服务，我们需定制bash shell环境，这里讲述bash用户环境配置。 bash_profile、.bashrc、和.bash_logout 上面这三个文件是bash shell的用户环境配置文件，位于用户的主目录下。注意bash_profile、.bash_logout并不总是存在，至少Debian中就没有默认创建 一、shell的启动方式 根据shell的启动方式不同，分为登录shell、交互shell和非交互shell，不同方式启动的shell初始化过程是不同的。 1.登录shell(login shell) 当我们输入用户名和密码登录到系统后，所呈现给我们的就是一个登录shell。更确切的说，如果启动bash对话的命令行第0个参数的首字符是&#8221;-&#8221;的话，那么这是一个登录shell； 可以通过如果命令看到 系统管理员工具包: 充分利用 bashgt; ps -f 还有一种是带有&#8221;&#8211;login&#8221;选项启动的bash对话是一个登录shell。 系统管理员工具包: 充分利用 bashgt; bash &#8211;login 2.交互shell(interactive shell) 带有&#8221;-i&#8221;选项启动的bash对话是一个交互shell。如： 系统管理员工具包: 充分利用 bashgt; bash -i 或者以非选项参数开始，并且不包含&#8221;-c&#8221;选项启动的bash对话，且它的标准输入和标准出错都是连接到终端的，那么这也是一个交互shell。 系统管理员工具包: 充分利用 bashgt; bash 3.非交互shell 用于启行一个脚本的shell是非交互shell 系统管理员工具包: 充分利用 bashgt; bash my.sh 二、登录shell的初始化文件 /etc/profile； ~/.bash_profile、~/.bash_login和~/.profile三个最先找到的那个； ~/.bash_logout，shell退出时执行。 三、交互shell的初始化 /etc/bash.bashrc ~/.bashrc 四、非交互shell的初始化 环境变量&#8221;BASH_ENV&#8221;代表的文件。 .bash_profile 其中.bash_profile是最重要的一个配置文件，它在用户每次登录系统时被读取，里面的所有 命令都会被bash执行。.profile(由Bourne Shell和Korn [...]]]></description>
			<content:encoded><![CDATA[<p>Bash是Debian的默认SHELL，设置终端环境（bash）使Bash更好地为我们服务，我们需定制bash shell环境，这里讲述bash用户环境配置。</p>
<p>bash_profile、.bashrc、和.bash_logout<br />
 上面这三个文件是bash shell的用户环境配置文件，位于用户的主目录下。注意bash_profile、.bash_logout并不总是存在，至少Debian中就没有默认创建</p>
<p><span id="more-185"></span></p>
<blockquote><p>一、shell的启动方式<br />
 根据shell的启动方式不同，分为登录shell、交互shell和非交互shell，不同方式启动的shell初始化过程是不同的。<br />
 1.登录shell(login shell)<br />
 当我们输入用户名和密码登录到系统后，所呈现给我们的就是一个登录shell。更确切的说，如果启动bash对话的命令行第0个参数的首字符是&#8221;-&#8221;的话，那么这是一个登录shell；<br />
 可以通过如果命令看到<br />
 系统管理员工具包: 充分利用 bashgt; ps -f<br />
 还有一种是带有&#8221;&#8211;login&#8221;选项启动的bash对话是一个登录shell。<br />
 系统管理员工具包: 充分利用 bashgt; bash &#8211;login</p>
<p>2.交互shell(interactive shell)<br />
 带有&#8221;-i&#8221;选项启动的bash对话是一个交互shell。如：<br />
 系统管理员工具包: 充分利用 bashgt; bash -i<br />
 或者以非选项参数开始，并且不包含&#8221;-c&#8221;选项启动的bash对话，且它的标准输入和标准出错都是连接到终端的，那么这也是一个交互shell。<br />
 系统管理员工具包: 充分利用 bashgt; bash<br />
 3.非交互shell<br />
 用于启行一个脚本的shell是非交互shell<br />
 系统管理员工具包: 充分利用 bashgt; bash my.sh<br />
 二、登录shell的初始化文件<br />
 /etc/profile；<br />
 ~/.bash_profile、~/.bash_login和~/.profile三个最先找到的那个；<br />
 ~/.bash_logout，shell退出时执行。<br />
 三、交互shell的初始化<br />
 /etc/bash.bashrc<br />
 ~/.bashrc<br />
 四、非交互shell的初始化<br />
 环境变量&#8221;BASH_ENV&#8221;代表的文件。</p>
</blockquote>
<p><strong>.bash_profile</strong></p>
<p>其中.bash_profile是最重要的一个配置文件，它在用户每次登录系统时被读取，里面的所有 命令都会被bash执行。.profile(由Bourne Shell和Korn Shell使用)和.login(由C Shell使用)两个文件是.bash_profile的同义词，目的是为了兼容其它Shell。在Debian中使用.profile文件代 替.bash_profile文件。</p>
<p><strong>.bashrc</strong></p>
<p>文件会在bash shell调用另一个bash shell时读取，也就是在shell中再键入bash命令启动一个新shell时就会去读该文件。这样可有效分离登录和子shell所需的环境。但一般 来说都会在.bash_profile里调用.bashrc脚本以便统一配置用户环境。</p>
<p><strong>.bash_logout</strong></p>
<p>在退出shell时被读取。所以我们可把一些清理工作的命令放到这文件中。</p>
<p><!--more--></p>
<p>在 /etc目录的bash.bashrc和profile是系统级（全局）的配置文件，当在用户主目录下找不到.bash_profile 和.bashrc\时，就会读取这两个文件。.bash_history是bash shell的历史记录文件，里面记录了你在bash shell中输入的所有命令。可通过HISSIZE环境变量设置在历史记录文件里保存记录的条数。alias l = &#8216;ls -l&#8217;是设置别名的语句，把它放在这些配置文档中就可使我们能用简单的&#8217;l'命令，代替&#8217;ls -l&#8217;命令。</p>
<p>当我们修改了这些配置件后，可用 <strong>source .bash_profile</strong> / <strong>source .</strong><strong>bashrc </strong>命令使它修改内容马上生效。</p>
<p><br class="spacer_" /></p>
<p>非管理员推荐通过编辑用户目录下的 ~/.bashrc 来设定Bash</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">vim</span> ~<span style="color: #000000; font-weight: bold;">/</span>.bashr         <span style="color: #666666; font-style: italic;">#编辑.bashr</span>
<span style="color: #7a0874; font-weight: bold;">source</span> .bashr        <span style="color: #666666; font-style: italic;">#重载Bash，使配置生效</span></pre></div></div>

<p lang="bash">如果管理员希望配置全局性的一些设定，首先要确定你理解了上述3种SHELL启动方式含义，推荐创建文件 /etc/bash.global 文件，内容如下。</p>
<p lang="bash">并确保用户可以加载到这一文件。简单的处理方法通过 /etc/profile 和 /etc/bash.bashrc调用</p>
<p>这是一个简单的设置，启用了目录彩色显示，修改了命令提示符的格式，改写了几个命令(rm,cp,mv)的默认选项</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#---- (lee)</span>
<span style="color: #666666; font-style: italic;"># /etc/bashrc.global: for all shells.</span>
<span style="color: #666666; font-style: italic;"># Add this lines to /etc/profile &amp;amp; /etc/bash.bashrc</span>
<span style="color: #666666; font-style: italic;"># # inlucde global setting, --(lee)</span>
<span style="color: #666666; font-style: italic;"># if [ -f /etc/bash.global ]; then</span>
<span style="color: #666666; font-style: italic;">#     . /etc/bash.global</span>
<span style="color: #666666; font-style: italic;"># fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#命令提示行</span>
<span style="color: #666666; font-style: italic;">#Promote</span>
<span style="color: #007800;">PS1</span>=<span style="color: #ff0000;">'-------------------------------------\n\u@\h:\w\$ '</span>
<span style="color: #666666; font-style: italic;">#Color Promote</span>
<span style="color: #007800;">PS1</span>=<span style="color: #ff0000;">'\[\033[01;32m\]       -------------------------------------------------------\[\033[00m\]\n${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># You may uncomment the following lines if you want `ls' to be colorized:</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LS_OPTIONS</span>=<span style="color: #ff0000;">'--color=auto'</span>
<span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #ff0000;">&quot;<span style="color: #780078;">`dircolors`</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">ls</span>=<span style="color: #ff0000;">'ls $LS_OPTIONS'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">ll</span>=<span style="color: #ff0000;">'ls $LS_OPTIONS -l'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">l</span>=<span style="color: #ff0000;">'ls $LS_OPTIONS -lA'</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Some more alias to avoid making mistakes:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">rm</span>=<span style="color: #ff0000;">'rm -i'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">cp</span>=<span style="color: #ff0000;">'cp -i'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mv</span>=<span style="color: #ff0000;">'mv -i'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">vi</span>=<span style="color: #ff0000;">'vim'</span></pre></div></div>

<p><strong>其它可以设置的选项</strong></p>
<p>bash shell中的选项可控制shell的行为和功能，我们可以通过shopt命令来设置。使用set命令也可以，但它已被shopt替代，但为了向下兼 容，set命令还是可以使用的。使用不带参数的shopt命令可以列出当前shell中只能由shopt设置的选项，用shopt -o可列出可由set命令设置的选项。</p>
<p>这些设置你既可以随时通过命令行进行，也可以添加到上面的配置文件中</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#下面是一些可由set命令基本的选项，默认是关闭的。</span>
emacs                  进入emacs编辑模式
<span style="color: #c20cb9; font-weight: bold;">vi</span>                     进入<span style="color: #c20cb9; font-weight: bold;">vi</span>编辑模式
ignoreeof              不允许单独使用Ctrl_D退出的用法，要使用<span style="color: #7a0874; font-weight: bold;">exit</span>。与<span style="color: #007800;">IGNOREEOF</span>=<span style="color: #000000;">10</span>等价
noclobber              不允许重定向覆盖已存在文件
noglob                 不允许扩展文件名通配符
nounset                使用未定义的变量时给出错误
&nbsp;
<span style="color: #666666; font-style: italic;">#下面是一些只能由shopt命令设置的选项</span>
cdspell          自动改正<span style="color: #7a0874; font-weight: bold;">cd</span>命令参数中的小错误
hostcomplete     以<span style="color: #000000; font-weight: bold;">@</span>开头时，按tab键可进行主机名的自动完成
dotgblob         以点开始的文件名被包含在路径名扩展中
mailwarn         显示邮件警告信息</pre></div></div>

<p><br class="spacer_" /></p>
<p>shopt命令的选项如下：</p>
<p style="padding-left: 30px;">-p          显示可设置选项及当前取值<br />
 -s          设置每一选项为on<br />
 -u          设置每一选项为off<br />
 -q          不输出信息<br />
 -o</p>
<p><br class="spacer_" /></p>
<p><strong>命令提示符</strong></p>
<p>命令提示符就是在linux的命令行界面每行开始的的系统提示字符，类似 <strong>Debian:/var#</strong></p>
<p>配置文件中export PS1=的内容是设定命令提示符（promote ）的格式，可用的字符代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">       \a     an ASCII bell character <span style="color: #7a0874; font-weight: bold;">&#40;</span>07<span style="color: #7a0874; font-weight: bold;">&#41;</span>
       \d     the  <span style="color: #c20cb9; font-weight: bold;">date</span>  <span style="color: #000000; font-weight: bold;">in</span>  <span style="color: #ff0000;">&quot;Weekday  Month  Date&quot;</span> format
              <span style="color: #7a0874; font-weight: bold;">&#40;</span>e.g., <span style="color: #ff0000;">&quot;Tue May 26&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
       \e     an ASCII escape character <span style="color: #7a0874; font-weight: bold;">&#40;</span>033<span style="color: #7a0874; font-weight: bold;">&#41;</span>
       \h     the <span style="color: #c20cb9; font-weight: bold;">hostname</span> up to the first <span style="color: #000000; font-weight: bold;">`</span>.<span style="color: #ff0000;">'
       \H     the hostname
       \n     newline
       \r     carriage return
       \s     the name of the shell, the  basename  of  $0
              (the portion following the final slash)
       \t     the current time in 24-hour HH:MM:SS format
       \T     the current time in 12-hour HH:MM:SS format
       \@     the current time in 12-hour am/pm format
       \u     the username of the current user
       \v     the version of bash (e.g., 2.00)
       \V     the  release  of  bash, version + patchlevel
              (e.g., 2.00.0)
       \w     the current working directory
       \W     the basename of the current  working  direc-
              tory
       \!     the history number of this command
       \#     the command number of this command
       \$     if  the effective UID is 0, a #, otherwise a
              $</span></pre></div></div>

<p>参考：</p>
<ul>
<li><a href="http://www.ibm.com/developerworks/cn/aix/library/au-satbash.html" target="_blank">系统管理员工具包: 充分利用 bash</a></li>
<li><a href="http://www.linuxselfhelp.com/howtos/Bash-Prompt/Bash-Prompt-HOWTO-2.html" target="_blank">http://www.linuxselfhelp.com/howtos/Bash-Prompt/Bash-Prompt-HOWTO-2.html</a></li>
</ul>
<p><a href="http://www.ibm.com/developerworks/cn/aix/library/au-satbash.html" target="_blank"><br />
 </a></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
]]></content:encoded>
			<wfw:commentRss>http://lee.kometo.com/archives/185/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

