0

I came across an interesting thing in Shell Scripting and not 100% sure why the behaviour is like this

I tried the below script:

#!/bin/sh
CMD="curl -XGET http://../endpoint";
var1=eval $CMD | sed -e 's/find/replace/g';
echo $var1;  # Output: printed the value on this line
echo $var1;  # Output: blank/no data printed (Why it is blank?)

I had to change the command in variable enclosing with back-tick ` to print the variable as many time as I wanted.

CMD="curl -XGET http://../endpoint";
var1=`eval $CMD | sed -e 's/find/replace/g'`;
echo $var1;  # Output: printed the value on this line 
echo $var1;  # Output: printed the value on this line 
  • Why I have to surround my command with ` to assign it's o/p to the variable in subsequent variable usage?

I have a feeling that it has something to do with the variable-command scope.

Shedding light on my understanding will be appreciated!

UPDATE: I tried the below command and it is working in my env.

#!/bin/sh
CMD="curl -XGET http://www.google.com/";
var1=eval $CMD | sed -e 's/find/replace/g';
echo $var1;  # Output: printed the value on this line
echo "######";
echo $var1;  # Output: blank/no data printed (Why it is blank?)
2
  • Note that the '$(...)' syntax is preferred over the backtick syntax. Here is an explanation: mywiki.wooledge.org/BashFAQ/082 Commented Feb 18, 2015 at 1:48
  • Thanks pmohandas for the suggestion. Commented Feb 18, 2015 at 1:55

2 Answers 2

2

sh/bash allows you to run a command with a variable in its environment, without permanently modifying the variable in the shell. This is great, because you can e.g. run a command in a certain language just one time without having to change your entire user's or system's language:

$ LC_ALL=en_US.utf8 ls foo
ls: cannot access foo: No such file or directory
$ LC_ALL=nb_NO.utf8 ls foo
ls: cannot access foo: Ingen slik fil eller filkatalog

However, this means that when you try to do

var=this is some command

you're trigger this syntax.

It means "run the command is a command and tell it that the variable var is set to this"

It does not assign "this is my string" to the variable, and it definitely does not evaluate "this is a string" as a command, and then assign its output to var.

Given this, we can look at what actually happened:

CMD="curl -XGET http://../endpoint";
var1=eval $CMD | sed -e 's/find/replace/g';  # No assignment, output to screen
echo $var1;  # Output: blank/no data printed
echo $var1;  # Output: blank/no data printed

There is no scope issue and no inconsistency: the variable is never assigned, and is never written by an echo statement.

var=`some command` (or preferably, var=$(some command)) works because this is valid syntax to assign output from a program to a variable.

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

2 Comments

aaha, if that is the case then let me run a test to confirm my understanding. Thannks. I will let you know.
Awesome! Thanks. That 'echo' statement confused me. When I remove 'echo' I still see command o/p. This makes sense now. Thank!
1

The first example isn't doing what you think it is.

Neither echo is printing anything. Make them echo "[$var1]" to see that.

You need the backticks to run the command and capture its output.

Your first attempt was running the $CMD | sed -e 's/find/replace/g'; pipeline with the environment of $CMD containing var1 set to a value of eval.

You also shouldn't be putting commands inside strings (or using eval in general). See http://mywiki.wooledge.org/BashFAQ/001 for more on why.

2 Comments

Don't play games with echo. Run the lines like I said so you can see what each echo outputs. Alternatively, put a sleep between the $CMD line and the first echo line. The echo is not printing anything.
Thanks Etan! I understood it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.