1 数组的格式
1.1 用()将数字元素扩起来,元素之间用空格间隔
hiyanlou:~/ $ arr=(1 2 3 4 5 6) //定义数组变量 [22:56:08]
shiyanlou:~/ $ echo $arr //打印数组所有元素,方式1,有的bash执行这一句只会打印第一个元素。 [22:58:04]
1 2 3 4 5 6
shiyanlou:~/ $ echo ${arr[@]} //打印数组所有元素,方式2 [22:58:10]
1 2 3 4 5 6
shiyanlou:~/ $ echo ${arr[*]} //打印数组所有元素,方式3 [22:58:32]
1 2 3 4 5 6
shiyanlou:~/ $ echo ${#arr[*]} //打印数组的长度 [22:58:39]
6
hiyanlou:~/ $ echo ${arr[0]} //数组元素的下标从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]} //负数的下标表示从右往左,从-1开始 [19:23:13]
6
shiyanlou:~/ $ echo ${arr[-2]} [19:23:31]
5
shiyanlou:~/ $
1.2 元素为反引号括起来的命令时,将命令执行后的结果按空格间隔存放进数组
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:~/ $