使用命令行输出时获取语法错误

发布于 2025-01-30 16:20:06 字数 241 浏览 4 评论 0原文

我正在尝试使用命令将输出存储在下面的脚本中,

#!/bin/bash

OUTPUT=${ls -al}
echo "$OUTPUT"

当我尝试执行时,

(localhost)$ ./subst.sh
./subst.sh: line 3: ${ls -al}: bad substitution

我会遇到以下语法错误:我在这里做错了什么?

I am trying to use command to store the output with the below script

#!/bin/bash

OUTPUT=${ls -al}
echo "$OUTPUT"

when I am trying to execute I am getting the below syntax error:

(localhost)$ ./subst.sh
./subst.sh: line 3: ${ls -al}: bad substitution

What is the wrong I am doing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2025-02-06 16:20:06

您需要更改卷发括号并使用括号,因为

卷曲括号用于可变更改,并且用于命令替换,您需要在脚本中使用括号

#!/bin/bash

OUTPUT=$(ls -al)
echo "$OUTPUT"

You need to change the Curly braces and use Parenthesis because

Curly braces are used for variable change and for command substitution you need to use Parenthesis in your script as below

#!/bin/bash

OUTPUT=$(ls -al)
echo "$OUTPUT"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文