将命令的输出分配给变量
问题描述:
我必须在空格后剪切字符串,并将值存储在空格前.我的示例脚本如下所示
I have to cut the string after the white-space and store the value before white-space. My example script is show below
tString="This is my name"
echo $tString | cut -d' ' -f1
输出:
此
现在,我想将此输出值分配给变量.我的脚本是
Now I want to assign this output value to the variable. My script is
tString="This is my name"
var=$($tString | cut -d' ' -f1)
它显示错误.错误消息是
It shows error.Error message is
此:找不到命令
我是bash shell脚本的新手.任何人都知道该怎么做.
Iam new to bash shell script. Anyone Knows how to do this.
答
添加 echo
:
tString="This is my name"
var=$(echo $tString | cut -d' ' -f1)
(也提到了此处我发帖前2秒我的答案)
(Also mentioned here 2 seconds before I posted my answer)