[bash] Some Tips for the Bourne Again Shell

本文探讨了Bash命令行中变量赋值、参数传递、路径名展开、PATH环境变量设置及历史记录相关环境变量的细节。通过具体示例说明了如何在Bash中正确处理变量和环境配置。

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

### Bash (Bourne-Again Shell) 的功能与使用指南 BashBourne-Again Shell)是一种广泛使用的命令行解释器,最初由 Brian Fox 于 1989 年开发,作为 Bourne Shell (sh) 的增强版本。Bash 是大多数 Linux 发行版和 macOS(2019 年之前)的默认 Shell,因其强大的功能和灵活性,广泛用于脚本编写和系统管理[^1]。 #### Bash 的主要功能 Bash 提供了丰富的功能,使其成为系统管理和脚本编写的强大工具。Bash 的命令语法是 Bourne Shell 命令语法的超集,这意味着数量庞大的 Bourne Shell 脚本大多可以不经修改就在 Bash 中执行。只有那些引用了 Bourne 特殊变量或使用了 Bourne 内置命令的脚本才需要修改。Bash 的许多命令语法特性来自 Korn Shell (ksh) 和 C Shell (csh),例如命令行编辑、命令历史、目录栈、$RANDOM 和 $PPID 变量,以及 POSIX 的命令置换语法:`$(…)`。作为一个交互式的 ShellBash 支持通过按下 TAB 键自动补全已部分输入的程序名、文件名、变量名等[^3]。 #### Bash 的基本使用方法 Bash 的使用方法主要包括命令行操作和脚本编写。在命令行中,用户可以直接输入命令来执行各种操作,如文件管理、进程控制等。Bash 还支持管道和重定向,使得用户可以将多个命令组合在一起,实现复杂的功能。 在脚本编写方面,Bash 提供了丰富的语法和结构,支持变量、条件判断、循环、函数等编程概念。以下是一个简单的 Bash 脚本示例,展示了如何定义和调用函数: ```bash #!/bin/bash # 定义一个函数 function greet { echo "Hello, $1!" } # 调用函数 greet "World" ``` 在这个示例中,`greet` 函数接受一个参数,并输出一条问候语。函数的定义和调用都非常简单,展示了 Bash 脚本的基本结构。 #### Bash 脚本编写方法 Bash 脚本的编写需要注意一些特定的规则和最佳实践。首先,Bash 脚本是逐行运行的,不会像其他语言一样先预编译。因此,必须在使用函数之前先声明函数。其次,Bash 中的函数可以看作是定义一个新的命令,各个输入参数直接用空格分隔。命令中获取参数的方法可以通过 `$0...$n` 得到,其中 `$0` 代表函数本身。函数的返回值只能通过 `$?` 系统变量获得,直接通过 `=` 获得的是空值[^4]。 此外,Bash 还支持一些高级特性,如数组、字符串操作、条件测试等。这些特性使得 Bash 脚本可以处理复杂的任务,如自动化系统管理、日志分析等。 #### Bash 的实际应用 Bash 在实际应用中非常广泛,尤其是在系统管理和自动化任务中。例如,用户可以使用 Bash 脚本来自动化备份操作、监控系统性能、管理用户账户等。以下是一个简单的 Bash 脚本示例,展示了如何使用 `mkdir` 快速创建多个目录: ```bash #!/bin/bash # 创建多个目录 mkdir -p test/{1,2,3,4,5,6,7}/{a,b,c,d,e,f,g} mkdir -p /home/USER/Desktop/tecmint/{etc/x1,lib,usr/{x2,x3},bin,tmp/{Y1,Y2,Y3/z},opt,var} ``` 这个脚本使用 `mkdir -p` 命令快速创建多个嵌套目录,非常适合在 Shell 脚本中使用。 #### 总结 Bash 是一个功能强大且灵活的命令行解释器,广泛用于 Linux 和 macOS 系统中。它不仅支持丰富的命令行操作,还提供了强大的脚本编写功能,使得用户可以轻松实现自动化任务和系统管理。通过学习和掌握 Bash 的基本语法和高级特性,用户可以显著提高工作效率和系统管理能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值