4

Why does the standard output from the following commands differ?

command1:

for ((i=0; i<10; i=i+1)); do echo $i; done

command2:

bash -c "for ((i=0; i<10; i=i+1)); do echo $i; done"

command1 prints the integers 1 to 10 on a separate line, as expected. command2 prints 10 on each line.

What do I need to do to get command2 to print the same thing as command1?

The long story:

I often need to run an executable on a large number of files and capture the output of the entire process. I use a for loop similar to command1 in a separate file called my_script.sh.

I can then get the output using:

bash my_script.sh > results.txt

Since it's a bit of a pain to create a separate my_script.sh file for trivial for loops, I was hoping to achieve the same thing by using "bash -c", but ran into this problem.

Thanks for any suggestions. Looking forward to replies.
Misha

1 Answer 1

14

What do I need to do to get command2 to print the same thing as command1?

You need to fix this line:

bash -c "for ((i=0; i<10; i=i+1)); do echo $i; done"

To say this instead:

bash -c "for ((i=0; i<10; i=i+1)); do echo \$i; done"

What's happening is that the $i sequence is being interpreted by the shell you are running bash from, and it is substituting its own i variable into the string before passing it off to the new bash instance to be executed. So by the time the new bash sees the string, it is:

for ((i=0; i<10; i=i+1)); do echo 10; done

Assuming that i contained the value "10".

(When I first ran the second script, which I did before running the first, it wrote out 10 blank lines, because I had no i variable defined, and so it was substituting in the empty string.)

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

6 Comments

Or you could just change the double quotes to single quotes.
Yup, either approach will work. Anything that gets the outer shell to treat the $ literally.
$i would be 10 if you ran command 2 after command 1, of course.
Thank you. Using the \ escape and using single quotes instead of double quotes both solve the problem.
@misha: On StackOverflow, "Thank you. That solves my problem" is expressed as a green tick to the left of the answer. Occasionally with an upvote.
|

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.