2

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.

4
  • 4
    var=$(echo $tString | cut -d' ' -f1) Commented May 15, 2013 at 8:25
  • 1
    Besides, you don't need this complex command. read var unused <<< $tString will also serve the purpose. Commented May 15, 2013 at 8:26
  • @anishsane,thanks both command works Commented May 15, 2013 at 9:08
  • Sorry, it should have been read var unused <<< "$tString" , else it may give errors on some bash'es... Commented May 15, 2013 at 9:47

2 Answers 2

9

Add an echo:

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

Using parameter expansion:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.