Question

How can i cut first word from a string, and then result store to variable?

So in the input i have String with 5 words.

Output will be 4 words without first one and must be stored in variable.

Already tried echo "First Second Third Fourth Fifth" | cut -d " " -f2- but i can't store result in variable.

Also i don't wont to see result of executing.

Was it helpful?

Solution

Use command substitution like this:

result=$(echo "First Second Third Fourth Fifth" | cut -d " " -f2-)

echo "$result"

OR using pure BASH:

s="First Second Third Fourth Fifth"
echo "${s#* }"

OUTPUT:

Second Third Fourth Fifth

OTHER TIPS

What I would prefer myself, would be to have the string in a text file, and then you can do this:-

read first second third fourth fifth < file

Then you can simply use:-

echo "${first}"
etc

Read is awesome. :D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top