Dinesh Subedi
Dinesh Subedi

Reputation: 2673

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

This: command not found

Iam new to bash shell script. Anyone Knows how to do this.

Upvotes: 2

Views: 5808

Answers (2)

jasonwryan
jasonwryan

Reputation: 4554

Using parameter expansion:

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

Upvotes: 1

speakr
speakr

Reputation: 4209

Add an echo:

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

(Also mentioned here 2 seconds before I posted my answer)

Upvotes: 9

Related Questions