Sachin
Sachin

Reputation: 1218

Assign result of command in a variable

I got this strange result today while assigning result of a command in a variable.

This command:

git branch | grep 480 

gives me result like this:

branch_name_480

given that branch_name_480 is the only branch with 480 in it.

But when I try to do this:

temp=`git branch | grep 480`

Or this:

temp=$(git branch | grep 480)

and after that: echo $temp this doesn't give me the expected result - which should be the same as before. Instead, this gives me result like all my directory listing and the expected result in a single line.

I know I can do this to get the expected result:

temp=$(echo 'git branch | grep 480')

So, my question is why is this happening? Why am I not getting the expected result before?

Upvotes: 2

Views: 896

Answers (1)

Joni
Joni

Reputation: 111239

Use echo "$temp".

The output from git branch includes an asterisk, which the shell expands to the directory listing. Quoting will prevent it from doing that.

Upvotes: 6

Related Questions