1 數(shù)組的格式
1.1 用()將數(shù)字元素擴起來矾飞,元素之間用空格間隔
hiyanlou:~/ $ arr=(1 2 3 4 5 6) //定義數(shù)組變量 [22:56:08]
shiyanlou:~/ $ echo $arr //打印數(shù)組所有元素,方式1柬赐,有的bash執(zhí)行這一句只會打印第一個元素。 [22:58:04]
1 2 3 4 5 6
shiyanlou:~/ $ echo ${arr[@]} //打印數(shù)組所有元素官紫,方式2 [22:58:10]
1 2 3 4 5 6
shiyanlou:~/ $ echo ${arr[*]} //打印數(shù)組所有元素肛宋,方式3 [22:58:32]
1 2 3 4 5 6
shiyanlou:~/ $ echo ${#arr[*]} //打印數(shù)組的長度 [22:58:39]
6
hiyanlou:~/ $ echo ${arr[0]} //數(shù)組元素的下標從1開始,有的bash從0開始 [22:58:47]
shiyanlou:~/ $ echo ${arr[1]} [23:00:46]
1
shiyanlou:~/ $ echo ${arr[6]} [23:00:54]
6
shiyanlou:~/ $ echo ${arr[-1]} //負數(shù)的下標表示從右往左束世,從-1開始 [19:23:13]
6
shiyanlou:~/ $ echo ${arr[-2]} [19:23:31]
5
shiyanlou:~/ $
1.2 元素為反引號括起來的命令時酝陈,將命令執(zhí)行后的結(jié)果按空格間隔存放進數(shù)組
shiyanlou:~/ $ ls [23:13:32]
anaconda3 Code Desktop
shiyanlou:~/ $ arr=(`ls`) [23:15:44]
shiyanlou:~/ $ echo ${arr[@]} [23:16:05]
anaconda3 Code Desktop
shiyanlou:~/ $ echo ${#arr[@]} [23:16:23]
3
shiyanlou:~/ $
shiyanlou:~/ $ la [23:16:40]
\u603b\u7528\u91cf 228K
lrwxrwxrwx 1 shiyanlou shiyanlou 10 6\u6708 9 2018 anaconda3 -> /anaconda3
-rw-r--r-- 1 shiyanlou shiyanlou 220 9\u6708 1 2015 .bash_logout
-rw-r--r-- 1 shiyanlou shiyanlou 3.7K 9\u6708 1 2015 .bashrc
drwxrwxr-x 5 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .cache
drwxrwxr-x 2 shiyanlou shiyanlou 4.0K 6\u6708 9 2018 Code
drwxrwxr-x 9 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .config
drwx------ 3 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .dbus
drwxr-xr-x 2 shiyanlou shiyanlou 4.0K 6\u6708 9 2018 Desktop
drwx------ 2 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .gconf
-rw-r--r-- 1 shiyanlou shiyanlou 155 6\u6708 9 2018 .gemrc
drwx------ 2 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .gvfs
-rw------- 1 shiyanlou shiyanlou 334 8\u6708 23 23:11 .ICEauthority
drwxrwxr-x 3 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .local
-rw-r--r-- 1 shiyanlou shiyanlou 41 6\u6708 9 2018 .npmrc
drwxrwxr-x 8 shiyanlou shiyanlou 4.0K 6\u6708 9 2018 .nvm
drwxr-xr-x 11 shiyanlou shiyanlou 4.0K 6\u6708 9 2018 .oh-my-zsh
drwxr-xr-x 2 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .pip
-rw-r--r-- 1 shiyanlou shiyanlou 655 5\u6708 16 2017 .profile
drwxrwxr-x 2 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .sogouinput
drwxr-x--- 4 shiyanlou shiyanlou 4.0K 6\u6708 9 2018 .vim
-rw------- 1 shiyanlou shiyanlou 598 6\u6708 9 2018 .viminfo
-rw-r--r-- 1 shiyanlou shiyanlou 6.5K 6\u6708 9 2018 .vimrc
drwxrwxr-x 2 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .vnc
-rw------- 1 shiyanlou shiyanlou 106 8\u6708 23 23:11 .Xauthority
-rw-rw-r-- 1 shiyanlou shiyanlou 14 6\u6708 9 2018 .xsession
-rw-rw-r-- 1 shiyanlou shiyanlou 39K 8\u6708 23 23:12 .zcompdump
-rw-rw-r-- 1 shiyanlou shiyanlou 39K 8\u6708 23 23:12 .zcompdump-1285c221a7fb-5.1.1
-rw-rw-r-- 1 shiyanlou shiyanlou 39K 6\u6708 9 2018 .zcompdump-4f7459d876a3-5.1.1
-rw------- 1 shiyanlou shiyanlou 338 8\u6708 23 23:19 .zsh_history
-rw-r--r-- 1 shiyanlou shiyanlou 3.3K 6\u6708 9 2018 .zshrc
shiyanlou:~/ $ arr3=(`la`) [23:20:05]
shiyanlou:~/ $ echo ${#arr3[@]} [23:20:21]
274
shiyanlou:~/ $ echo ${arr3[@]} // \uXXX 都是中文對應的編碼,拷貝出來就變成這樣了 [23:20:45]
\u603b\u7528\u91cf 228K lrwxrwxrwx 1 shiyanlou shiyanlou 10 6\u6708 9 2018 anaconda3 -> /anaconda3 -rw-r--r-- 1 shiyanlou shiyanlou 220 9\u6708 1 2015 .bash_logout -rw-r--r-- 1 shiyanlou shiyanlou 3.7K 9\u6708 1 2015 .bashrc drwxrwxr-x 5 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .cache drwxrwxr-x 2 shiyanlou shiyanlou 4.0K 6\u6708 9 2018 Code drwxrwxr-x 9 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .config drwx------ 3 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .dbus drwxr-xr-x 2 shiyanlou shiyanlou 4.0K 6\u6708 9 2018 Desktop drwx------ 2 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .gconf -rw-r--r-- 1 shiyanlou shiyanlou 155 6\u6708 9 2018 .gemrc drwx------ 2 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .gvfs -rw------- 1 shiyanlou shiyanlou 334 8\u6708 23 23:11 .ICEauthority drwxrwxr-x 3 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .local -rw-r--r-- 1 shiyanlou shiyanlou 41 6\u6708 9 2018 .npmrc drwxrwxr-x 8 shiyanlou shiyanlou 4.0K 6\u6708 9 2018 .nvm drwxr-xr-x 11 shiyanlou shiyanlou 4.0K 6\u6708 9 2018 .oh-my-zsh drwxr-xr-x 2 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .pip -rw-r--r-- 1 shiyanlou shiyanlou 655 5\u6708 16 2017 .profile drwxrwxr-x 2 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .sogouinput drwxr-x--- 4 shiyanlou shiyanlou 4.0K 6\u6708 9 2018 .vim -rw------- 1 shiyanlou shiyanlou 598 6\u6708 9 2018 .viminfo -rw-r--r-- 1 shiyanlou shiyanlou 6.5K 6\u6708 9 2018 .vimrc drwxrwxr-x 2 shiyanlou shiyanlou 4.0K 8\u6708 23 23:11 .vnc -rw------- 1 shiyanlou shiyanlou 106 8\u6708 23 23:11 .Xauthority -rw-rw-r-- 1 shiyanlou shiyanlou 14 6\u6708 9 2018 .xsession -rw-rw-r-- 1 shiyanlou shiyanlou 39K 8\u6708 23 23:12 .zcompdump -rw-rw-r-- 1 shiyanlou shiyanlou 39K 8\u6708 23 23:12 .zcompdump-1285c221a7fb-5.1.1 -rw-rw-r-- 1 shiyanlou shiyanlou 39K 6\u6708 9 2018 .zcompdump-4f7459d876a3-5.1.1 -rw------- 1 shiyanlou shiyanlou 406 8\u6708 23 23:20 .zsh_history -rw-r--r-- 1 shiyanlou shiyanlou 3.3K 6\u6708 9 2018 .zshrc
shiyanlou:~/ $