[bash] Some Tips for the Bourne Again Shell

1. bash命令行中的变量赋值语句只适用于该命令

This example comes from A practical guide to Fedora and Red Hat Enterprise Linux (6th ed.).

$ cat my_script
echo $TEMPDIR
$ TEMPDIR=/home/sam/temp ./my_script
/home/sam/temp
$ echo $TEMPDIR
$

2. bash在将变量作为参数传给其他程序时会自动压缩相邻的空格

如果想保留多个相邻的空格,则应该使用双引号将变量引起。

This example comes from Sobell (2011) [1] .

$ person="max    and    zach"
$ echo $person
max and zach
$ echo "$person"
max    and    zach

关于这段bash代码的解释 [2]

$echo $person

bash将$person替换为max and zach,所以上面这句代码等价于

$echo max    and    zach

bash在解释它的时候会将maxandzach作为三个参数传给echo,而echo并不知道中间空格,只是将这三个参数显示了而已。

$echo "$person"

bash将"$person"替换为"max and zach",所以上面这句代码等价于

$echo "max    and    zach"

bash只将一个参数"max and zach"传给了echo,所以结果如同例子中的那样。

3. bash在解释命令之前才将*展开

bash在赋值的时候并不对*进行路径名展开,而是在解释命令的时候进行此步骤,若想防止展开,可以使用双引号。

This example comes from A practical guide to Fedora and Red Hat Enterprise Linux (6th ed.).

$ ls
max.report
max.summary
$ memo=max*
$ echo $memo
max.report max.summary
$ echo "$memo"
max*

4. 四种将当前工作目录加如到PATH环境变量的方法

因为在PATH的值中,空值代表当前目录,所以,共有四种方法表示PATH中包含当前目录(虽然因为安全原因,并不推荐将当前目录加入到PATH中):

  1. 在最前面加入冒号(第一个值为空值):PATH=:/usr/local/bin:/bin:/usr/bin
  2. 在最后面加入冒号(最后一个值为空值):PATH=/usr/local/bin:/bin:/usr/bin:
  3. 加入连续两个冒号(两个冒号中间的值为空值):PATH=/usr/local/bin:/bin::/usr/bin
  4. 显示地指定.为当前目录:PATH=/usr/local/bin:/bin:/usr/bin:.

5. Environment variable connected with history

  • HISTCONTROL A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups. A value of erasedups causes all previous lines matching the current line to be removed from the history list before that line is saved. Any value not in the above list is ignored. If HISTCONTROL is unset, or does not include a valid value, all lines read by the shell parser are saved on the history list, subject to the value of HISTIGNORE. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL.
  • HISTFILE The name of the file in which command history is saved (see HISTORY below). The default value is ~/.bash_history. If unset, the command history is not saved when an interactive shell exits.
  • HISTIGNORE A colon-separated list of patterns used to decide which command lines should be saved on the history list. Each pattern is anchored at the beginning of the line and must match the complete line (no implicit `*' is appended). Each pattern is tested against the line after the checks specified by HISTCONTROL are applied. In addition to the normal shell pattern matching characters, `&' matches the previous history line. `&' may be escaped using a backslash; the backslash is removed before attempting a match. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTIGNORE.
  • HISTSIZEThe number of commands to remember in the command history (see HISTORY below). The default value is 500.
  • HISTTIMEFORMATIf this variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each history entry displayed by the history builtin. If this variable is set, time stamps are written to the history file so they may be preserved across shell sessions. This uses the history comment character to distinguish timestamps from other history lines.
Reference
  1. Sobell, M. (2011). A practical guide to Fedora and Red Hat Enterprise Linux (6th ed.). Upper Saddle River, NJ: Prentice Hall.
  2. [superuser.com] Why bash compress adjacent SPACEs into one?
  3. bash man page

转载于:https://www.cnblogs.com/unimous/archive/2012/10/27/2742196.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值