简体   繁体   English

将命令的输出分配给变量

[英]Assign the output of command to variable

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

output: 输出:

This 这个

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 它显示error.Error消息

This: command not found 这:找不到命令

Iam new to bash shell script. 我是bash shell脚本的新手。 Anyone Knows how to do this. 任何人都知道如何做到这一点。

Add an echo : 添加echo

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

(Also mentioned here 2 seconds before I posted my answer) (在我发布答案前2秒也提到这里

Using parameter expansion : 使用参数扩展

tString="This is my name"
var="${tString%% *}"
echo "$var"
This

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM