shell .sh eval 使用简介
-
1 开头 — #! /bin/bash
-
eval — 对命令二次扫描,确认为普通操作,相当于 echo,若扫描到其中有变量转换,则会将变量转换后显示
touch test.sh (创建名为test.sh的文件)
touch test.txt (创建名为test.txt的文件)
vim test.txt (编辑此文件)
txt文件内容
hhhhhh
vim test.sh (编辑此文件)
案例1(sh文件内容)
#! /bin/bash
w="cat test.txt"
echo $w
eval $w
案例2(sh文件内容)
#! /bin/bash
testfile='cat test.txt'
echo $testfile
eval echo $testfile
echo $(echo $testfile) # echo 没有扫描是否有变量置换功能
eval $(echo $testfile)
cat test.txt
$() 有置换变量功能
sh test.sh (运行此脚本文件)
案例1运行结果展示
cat test.txt
hhhhhh
案例2运行结果展示
cat test.txt
cat test.txt
cat test.txt
hhhhhh
hhhhhh