26

So, lets say I have the following command:

curl -I http://google.com | head -n 1| cut -d $' ' -f2

This will capture the http status code?? Now I want to assign this to variable.. in bash script

like output = "curl -I http://localhost:8088/tracks?key=9 | head -n 1| cut -d $' ' -f2" 

or something like that..

How do I assign the response of above command to a variable called output in bash? Thanks

1
  • 4
    try surrounding the command in back ticks Commented Aug 15, 2014 at 3:44

1 Answer 1

53

You have two options (see this StackOverflow answer here):

  • Preferred: Surround the invocation in $()
  • Surround the invocation in back ticks

NOTE: back ticks are legacy, the former method is preferred.

output=$(curl -I http://google.com | head -n 1| cut -d $' ' -f2)
echo "$output";

output=`curl -I http://google.com | head -n 1| cut -d $' ' -f2`
echo "$output";
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to return value of eval command? Nothing works from all tried on SO

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.