Linux shell 命令多行结果赋值给变量
赋值
两种方式 :
1、$()方式
data=$(ls -l)
2、单反引号方式
data=`ls -l`
读取
(1) 如果是echo $data,输出结果为一行,没有换行符:
echo $data
total 132 drwxrwxr-x 3 faster faster 4096 Mar 31 06:11 client drwxrwxr-x 2 faster faster 4096 Mar 31 06:11 common drwxrwxr-x 2 faster faster 4096 Sep 1 11:34 conf -rw-rw-r-- 1 faster faster 35067 Dec 29 2016 COPYING-3_0.txt -rw-rw-r-- 1 faster faster 2959 Dec 29 2016 fastdfs.spec -rw-rw-r-- 1 faster faster 32463 Dec 29 2016 HISTORY drwxrwxr-x 2 faster faster 4096 Dec 29 2016 init.d -rw-rw-r-- 1 faster faster 7755 Dec 29 2016 INSTALL -rwxrwxr-x 1 faster faster 5548 Dec 29 2016 make.sh drwxrwxr-x 2 faster faster 4096 Dec 29 2016 php_client -rw-rw-r-- 1 faster faster 2380 Dec 29 2016 README.md -rwxrwxr-x 1 faster faster 1768 Dec 29 2016 restart.sh -rwxrwxr-x 1 faster faster 1680 Dec 29 2016 stop.sh drwxrwxr-x 4 faster faster 4096 Mar 31 06:11 storage drwxrwxr-x 2 faster faster 4096 Dec 29 2016 test drwxrwxr-x 2 faster faster 4096 Mar 31 06:11 tracker
(2) 如果是echo "$data",输出结果为多行,有换行符:
echo "$data"
total 132
drwxrwxr-x 3 faster faster 4096 Mar 31 06:11 client
drwxrwxr-x 2 faster faster 4096 Mar 31 06:11 common
drwxrwxr-x 2 faster faster 4096 Sep 1 11:34 conf
-rw-rw-r-- 1 faster faster 35067 Dec 29 2016 COPYING-3_0.txt
-rw-rw-r-- 1 faster faster 2959 Dec 29 2016 fastdfs.spec
-rw-rw-r-- 1 faster faster 32463 Dec 29 2016 HISTORY
drwxrwxr-x 2 faster faster 4096 Dec 29 2016 init.d
-rw-rw-r-- 1 faster faster 7755 Dec 29 2016 INSTALL
-rwxrwxr-x 1 faster faster 5548 Dec 29 2016 make.sh
drwxrwxr-x 2 faster faster 4096 Dec 29 2016 php_client
-rw-rw-r-- 1 faster faster 2380 Dec 29 2016 README.md
-rwxrwxr-x 1 faster faster 1768 Dec 29 2016 restart.sh
-rwxrwxr-x 1 faster faster 1680 Dec 29 2016 stop.sh
drwxrwxr-x 4 faster faster 4096 Mar 31 06:11 storage
drwxrwxr-x 2 faster faster 4096 Dec 29 2016 test
drwxrwxr-x 2 faster faster 4096 Mar 31 06:11 tracker