把命令结果作为变量赋值

1、变量名=`ls`    用反引号引起来

2、变量名=$(ls)  用$()括起来,推荐使用此方法

yao@linux:~/shells$ PWD=$(pwd)
yao@linux:~/shells$ echo $PWD
/home/yao/shells

yao@linux:~/shells$ user=`whoami`
yao@linux:~/shells$ echo $user
yao

例1:按日期打包目录

yao@linux:~/shells$ tar zcf shells_$(date +%F).tar.gz .
yao@linux:~/shells$ ls shells_2019-10-20.tar.gz
shells_2019-10-20.tar.gz

猜你喜欢

转载自www.cnblogs.com/yaowensheng/p/11706801.html
今日推荐