linux命令替换

来源:互联网 发布:富安娜淘宝旗舰店 编辑:程序博客网 时间:2024/06/05 10:52

从命令输出中提取信息

test=`date`echo $test20170912日 星期二 21:18:07 CST
test=$(date)echo $test20170912日 星期二 21:18:07 CST

demo

#!/bin/bash#copy the /user/bin directory listing to a log filetoday=$(date +%y%m%d)ls /usr/bin -al > log.$today

命令替换会创建一个子shell来运行对应的命令。子shell是由运行该脚本的shell所创建出来的一个独立的子shell。正因为如此,由该子shell所执行命令是无法使用脚本中所创建的变量的。
在命令提示符下使用路径./运行命令的话,也会创建子shell;不加路径的话不会创建子shell

原创粉丝点击