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

相关推荐

  1. Day<span style='color:red;'>35</span>

    Day35

    2024-04-08 05:52:02      7 阅读
  2. 安卓UI面试题 31-35

    2024-04-08 05:52:02       18 阅读
  3. IOS面试题编程机制 31-35

    2024-04-08 05:52:02       17 阅读
  4. 力扣704/35/34:二分查找

    2024-04-08 05:52:02       14 阅读
  5. 题解:力扣704/35/34

    2024-04-08 05:52:02       13 阅读
  6. Redis面试题35

    2024-04-08 05:52:02       32 阅读
  7. Redis面试题35

    2024-04-08 05:52:02       40 阅读
  8. day35_js

    2024-04-08 05:52:02       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-08 05:52:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-08 05:52:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-08 05:52:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-08 05:52:02       20 阅读

热门阅读

  1. 李沐19_卷积层——自学笔记

    2024-04-08 05:52:02       13 阅读
  2. 146 LRU缓存

    2024-04-08 05:52:02       14 阅读
  3. HTTP的强制缓存和协商缓存

    2024-04-08 05:52:02       15 阅读
  4. HTTPS中的TLS和TCP能同时握手吗

    2024-04-08 05:52:02       15 阅读
  5. GMSSL学习笔记

    2024-04-08 05:52:02       13 阅读
  6. 网络安全之SQL注入

    2024-04-08 05:52:02       14 阅读
  7. ubuntu18.04-arm7v架构下构建Telegraf自定义系统服务

    2024-04-08 05:52:02       13 阅读
  8. ubuntu怎么按安装时间显示已安装的软件

    2024-04-08 05:52:02       14 阅读
  9. 使用docx4j转换word为pdf处理中文乱码问题

    2024-04-08 05:52:02       10 阅读