What is expansion

  1. $ echo This is a test.
  2. This is a test.
  3. $ echo *
  4. Desktop Pictures Templates
  5. Documents Music Public Videos

echo *

  • 首先 shell 会把 * 认为是 Pathname Expansion,得到当前目录所有的文件名;
  • 然后把文件名传递给 echo ,因此输出的不是 *, 而是一串文件名。

    Pathname Expansion

    $ echo D*
    Desktop Documents
    $ echo *s
    Documents Pictures Templates Videos
    $ echo [[:upper:]]*
    Desktop Documents Music Pictures Public Templates Videos
    $ echo /usr/*/share
    /usr/kerberos/share /usr/local/share
    
    See also Wildcards

    Tilde Expansion

    $ echo ~
    /home/ronnie
    $ cd ~ # change directory to /home/ronnie
    $ cd ~jinyuan # change directory to /home/jinyuan
    

    Arithmetic Expansion

    **$((expression))**
    + - * / % ** ```bash $ echo $((2 + 2)) 4

$ echo $(($((52)) * 3)) 75 $ echo $(((52) * 3)) # Same as previous command 75

$ echo Five divided by two equals $((5/2)) Five divided by two equals 2 $ echo with $((5%2)) left over. with 1 left over.

See also [Arithmetic Evaluation and Expansion](https://www.yuque.com/liangxinketang/linux/zpqpd9)
<a name="qNtGN"></a>
## Brace Expansion
`**[preamble]{comma-separated|range|single}[postscript]**` 
```bash
$ echo Front-{A,B,C}-Back
Front-A-Back Front-B-Back Front-C-Back

$ echo Number_{1..5}
Number_1 Number_2 Number_3 Number_4 Number_5

$ echo {01..15}
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
$ echo {001..15}
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015

$ echo {Z..A}
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A

$ echo a{A{1,2},B{3,4}}b
aA1b aA2b aB3b aB4b

$ mkdir {2007..2009}-{01..12}
$ ls
2007-01 2007-07 2008-01 2008-07 2009-01 2009-07
2007-02 2007-08 2008-02 2008-08 2009-02 2009-08
2007-03 2007-09 2008-03 2008-09 2009-03 2009-09
2007-04 2007-10 2008-04 2008-10 2009-04 2009-10
2007-05 2007-11 2008-05 2008-11 2009-05 2009-11
2007-06 2007-12 2008-06 2008-12 2009-06 2009-12

Parameter Expansion

**$VARIABLE**
**${VARIABLE}**

$ echo $USER
ronnie
$ echo ${HOME}/*
/home/ronnie/bin /home/ronnie/Desktop /home/ronnie/Documents ...

See also Parameter Expansion

Command Substitution

**$(command)**

$ echo $(ls)
Desktop Documents Music Pictures Public Templates Videos

$ ls -l $(which cp)
-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 /bin/cp
$ file $(ls -d /usr/bin/* | grep zip)
/usr/bin/bunzip2: symbolic link to 'bzip2'
/usr/bin/bzip2: ELF 32-bit LSB executable, Intel 80386, version 1
...

There is an alternate syntax for command substitution in older shell programs: backquotes ``

$ ls -l `which cp`
-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 /bin/cp

Word Splitting

$ echo this is a     test
this is a test

Word splitting treats the following as delimiters between words:

  1. spaces
  2. tabs
  3. newlines (line feed characters)

This means unquoted spaces, tabs, and newlines are not considered to be part of the text. They serve only as separators. Because they separate the words into different arguments, previous command line contains a command( echo) followed by four distinct arguments.