3.5Shell_Expansions

Expansion is performed on the command line after it has been split into tokens.
There are seven kinds of expansion performed:
-brace expansion
-tilde expansion
-parameter and variable expansion
-command substitution
-arithmetic expansion
-word splitting
-filename expansion
The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion,
and command substitution(done in a left-to-right fashion); word splitting; and filename expansion.

After these expansions are performed, quote characters present in the original word are removed unless they have been
quoted themselves (quote removal).

Only brace expansion, word splitting, and filename expansion can increase the number of words of the expansion;
other expansions expand a single word to a single word.
The only exceptions to this are the expansions of "$@" and $*, and "${name[@]}" and ${name[*]} (see Arrays).

After all expansions, quote removal is performed.

Brace Expansion

echo a{d,c,b}e#ade ace abe
touch /mnt/{a,b,c}.txt#创建出a.txt,b.txt,c.txt三个文件

Tilde Expansion

If a word begins with an unquoted tilde character (‘~’), all of the characters up to the first unquoted slash
are considered a tilde-prefix.
If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde
are treated as a possible login name.

If the login name is invalid, or the tilde expansion fails, the word is left unchanged.
[root@kafka100 cpp]# echo ~
/root
[root@kafka100 cpp]# echo ~/aaa
/root/aaa

Shell Parameter Expansion

The basic form of parameter expansion is ${parameter}.The value of parameter is substituted.
The braces are required when parameter is a positional parameter with more than one digit, or when parameter is
followed by a character that is not to be interpreted as part of its name.
[root@kafka100 cpp]# echo $JAVA_HOMELLL

[root@kafka100 cpp]# echo ${JAVA_HOME}LLL
/usr/local/src/jdk1.8.0_333LLL

Command Substitution

$(command)或者`command`
Bash performs the expansion by executing command in a subshell environment 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 ‘\’.
When using the $(command) form,all characters between the parentheses make up the command;none are treated specially.
If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.

Command substitution allows the output of a command to replace the command itself.
#echo $(uname | $(echo cat))
=>echo $(uname | cat)
=>echo Linux
#echo `uname | \`echo cat\``

Filename Expansion

After word splitting, unless the -f option has been set(see The Set Builtin),Bash scans each word for the
characters ‘*’, ‘?’, and ‘[’.
If one of these characters appears,and is not quoted,then the word is regarded as a pattern,and replaced with
an alphabetically sorted list of filenames matching the pattern(see Pattern Matching).
If no matching filenames are found, and the shell option nullglob is disabled, the word is left unchanged.
If the shell option nocaseglob is enabled, the match is performed without regard to the case of alphabetic characters.

When a pattern is used for filename expansion,the character ‘.’ at the start of a filename or immediately following
a slash must be matched explicitly,unless the shell option dotglob is set.
In order to match the filenames ‘.’ and ‘..’,the pattern must begin with ‘.’(for example, ‘.?’),even if dotglob is set.

Pattern Matching

Any character that appears in a pattern, other than the special pattern characters described below, matches itself.
A backslash escapes the following character;the escaping backslash is discarded when matching.
The special pattern characters must be quoted if they are to be matched literally.
*
Matches any string, including the null string.
?
Matches any single character.
[]
Matches any one of the enclosed characters.
A pair of characters separated by a hyphen denotes a range expression;any character that falls between those two
characters, inclusive,using the current locale’s collating sequence and character set,is matched.
ls *#匹配到当前目录下的所有文件以及目录(没有.|..|隐藏文件|隐藏目录)
ls .?#匹配到上级目录..和(名字只有一个字符的隐藏文件和隐藏目录)
ls .和ls是一样的
shopt -s dotglob;ls * -d#此时匹配到当前目录下的所有文件以及目录(没有.|..)
echo r*#打印出来当前目录下以r开头的文件名和目录名;如果没有以r开头的文件名和目录名,则*不进行expansion,打印出原样的r*

http://mywiki.wooledge.org/BashFAQ/082
https://unix.stackexchange.com/questions/70928/shell-wildcards-and-dot-files
https://www.linfo.org/dot_slash.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值