gpt4 book ai didi

linux - 将命令的输出分配给变量

转载 作者:太空狗 更新时间:2023-10-29 11:27:31 25 4
gpt4 key购买 nike

我必须在空格之后剪切字符串并将值存储在空格之前。我的示例脚本如下所示

tString="This is my name"
echo $tString | cut -d' ' -f1

输出:

This

现在我想将这个输出值赋给变量。我的脚本是

tString="This is my name"
var=$($tString | cut -d' ' -f1)

显示错误,错误信息为

This: command not found

我是 bash shell 脚本的新手。任何人都知道如何执行此操作。

最佳答案

添加一个echo:

tString="This is my name"
var=$(echo $tString | cut -d' ' -f1)

(在我发布答案前 2 秒也提到了 here)

关于linux - 将命令的输出分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16560118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com