Bash的参数和参数扩展相关知识


不写多余的内容,只放3张表格,一目了然:

函数的 Shell 参数
参数 目的
0, 1, 2, … 位置参数从参数 0 开始。参数 0 引用启动 bash 的程序的名称,如果函数在 shell 脚本中运行,则引用 shell 脚本的名称。有关该参数的其他信息,比如 bash 由 -c 参数启动,请参阅 bash 手册页面。由单引号或双引号包围的字符串被作为一个参数进行传递,传递时会去掉引号。 如果是双引号,则在调用函数之前将对 $HOME 之类的 shell 变量进行扩展。对于包含嵌入空白或其他字符(这些空白或字符可能对 shell 有特殊意义)的参数,需要使用单引号或双引号进行传递。
* 位置参数从参数 1 开始。如果在双引号中进行扩展,则扩展就是一个词,由 IFS 特殊变量的第一个字符将参数分开,如果 IFS 为空,则没有间隔空格。IFS 的默认值是空白、制表符和换行符。如果没有设置 IFS,则使用空白作为分隔符(仅对默认 IFS 而言)。
@ 位置参数从参数 1 开始。如果在双引号中进行扩展,则每个参数都会成为一个词,因此 “$@” 与 “$1” “$2” 等效。如果参数有可能包含嵌入空白,那么您将需要使用这种形式。
# 参数数量(不包含参数 0)。

注意:如果您拥有的参数多于 9 个,则不能使用 $10 来引用第十个参数。首先,您必须处理或保存第一个参数($1),然后使用 shift 命令删除参数 1 并将所有剩余的参数下移一位,因此 $10 就变成了 $9,依此类推。$# 的值将被更新以反映参数的剩余数量。在实践中,最常见的情况是将参数迭代到函数或 shell 脚本,或者迭代到命令替换使用 for 语句创建的列表,因此这个约束基本不成问题。

==

Shell 扩展模式匹配
扩展 目的
${PARAMETER#WORD} shell 像文件名扩展中那样扩展 WORD,并从 PARAMETER 扩展后的值的开头删除最短的匹配模式(若存在匹配模式的话)。使用 ‘@’ 或 ‘$’ 即可删除列表中每个参数的模式。
${PARAMETER##WORD} 导致从开头删除最长的匹配模式而不是最短的匹配模式。
${PARAMETER%WORD} shell 像文件名扩展中那样扩展 WORD,并从 PARAMETER 扩展后的值末尾删除最短的匹配模式(若存在匹配模式的话)。使用 ‘@’ 或 ‘$’ 即可删除列表中每个参数的模式。
${PARAMETER%%WORD} 导致从末尾删除最长的匹配模式而不是最短的匹配模式。
${PARAMETER/PATTERN/STRING} shell 像文件名扩展中那样扩展 PATTERN,并替换 PARAMETER 扩展后的值中最长的匹配模式(若存在匹配模式的话)。为了在 PARAMETER 扩展后的值开头匹配模式,可以给 PATTERN 附上前缀 #,如果要在值末尾匹配模式,则附上前缀 %。如果 STRING 为空,则末尾的 / 可能被忽略,匹配将被删除。使用 ‘@’ 或 ‘$’ 即可对列表中的每个参数进行模式替换。
${PARAMETER//PATTERN/STRING} 对所有的匹配(而不只是第一个匹配)执行替换。

==

默认值相关的 Shell 扩展
扩展 目的
${PARAMETER:-WORD} 如果 PARAMETER 没有设置或者为空,则 shell 扩展 WORD 并替换结果。PARAMETER 的值没有更改。
${PARAMETER:=WORD} 如果 PARAMETER 没有设置或者为空,则 shell 扩展 WORD 并将结果指派给 PARAMETER。这个值然后被替换。不能用这种方式指派位置参数或特殊参数的值。
${PARAMETER:?WORD} 如果 PARAMETER 没有设置或者为空,shell 扩展 WORD 并将结果写入标准错误中。如果没有 WORD 则写入一条消息。如果 shell 不是交互式的,则表示存在这个扩展。
${PARAMETER:+WORD} 如果 PARAMETER 没有设置或者为空,则不作替换。否则 shell 扩展 WORD 并替换结果。
参考链接:

Linux 技巧: Bash 参数和参数扩展

====

Linux shell中的IFS变量
搜索关键字:
$IFS

临时修改IFS变量为换行符时(IFS变量的默认值为:空格、制表符、换行符),需要注意格式为:

IFS=$'\n'

The IFS is a special shell variable.

You can change the value of IFS as per your requirments.

The Internal Field Separator (IFS) 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>. You can print it with the following command:

cat -etv <<<"$IFS"

IFS variable is commonly used with read command, parameter expansions and command substitution.

From the bash man page:

The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words on these characters. If IFS is unset, or its value is exactly <space><tab><newline>, the default, then sequences of <space>, <tab>, and <newline> at the beginning and end of the results of the previous expansions are ignored, and any sequence of IFS characters not at the beginning or end serves to delimit words. If IFS has a value other than the default, then sequences of the whitespace characters space and tab are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character). Any character in IFS that is not IFS whitespace, along with any adjacent IFS whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter. If the value of IFS is null, no word splitting occurs.

The default value of IFS is a space, a tab, and a newline.

Command Substitution

Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed as follows:

$(command)

or

`command`

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by ‘$’, ‘`’, or ‘\’. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

Command substitutions may be nested. To nest when using the backquoted form, escape the inner backquotes with backslashes.

If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.

参考链接:
, ,

《“Bash的参数和参数扩展相关知识”》 有 1 条评论

  1. 为shell脚本中的变量设置默认值(用「:-」而不是「:=」)
    `
    #!/bin/bash
    # set -x
    interval=${1:-2}

    echo -e “parameter(s):\t” $@
    echo “interval = $interval”
    echo “\$1 = $1”
    `
    https://stackoverflow.com/questions/2013547/assigning-default-values-to-shell-variables-with-a-single-command-in-bash
    https://unix.stackexchange.com/questions/122845/using-a-b-for-variable-assignment-in-scripts
    https://www.cyberciti.biz/faq/bash-ksh-if-variable-is-not-defined-set-default-variable/

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注