linux - 在 Unix 中的变量中捕获命令行的输出

标签 linux bash unix

我想在变量中捕获下面命令的输出。

命令:

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/

相关文章:

linux - Buildroot 导出配置符号的位置

Bash:调用函数时没有返回值

用于清理旧备份文件的 Bash 脚本

linux - Bash 加入命令

mysql - 无法使用 RubyMine 连接到正确的 mysql 安装

Python 多处理 - AssertionError : can only join a child process

php - ACCESS_REFUSED - 使用身份验证机制 AMQPLAIN 拒绝登录

html - 使用 Linux -poppler-utils-pdftohtml 从 pdf 生成带有图像的 html

linux - 如何在 Eclipse 中使用 GDB 进行 C/C++ 调试?

macos - 如何修复在终端中显示 Android 主机名的 MacBook?