我想在变量中捕获下面命令的输出。
命令:
find . -iname 'FIL*'.TXT
输出是:
./FILE1.TXT
我想将“./FILE1.TXT”捕获到“A”变量中。但是当我尝试
A=`find . -iname 'FIL*'.TXT`
然后这个命令显示文件的数据。但是我想要变量 A 中的 ./FILE1.TXT
值。
最佳答案
# ls *.txt
test1.txt test.txt
# find ./ -maxdepth 1 -iname "*.txt"
./test1.txt
./test.txt
# A=$(find ./ -maxdepth 1 -iname "*.txt")
# echo $A
./test1.txt ./test.txt
如果你愿意,你可以忽略-maxdepth 1
。我不得不在这个例子中使用它。
或者使用单个文件:
# ls *.txt
test.txt
# find ./ -maxdepth 1 -iname "*.txt"
./test.txt
# A=$(find ./ -maxdepth 1 -iname "*.txt")
# echo $A
./test.txt
关于linux - 在 Unix 中的变量中捕获命令行的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20464010/