Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Explain the behavior of the following shell script:

$ cat quote_demo
twoliner="This is line 1.
This is line 2."
echo "$twoliner"
echo $twoliner
  • How many arguments does each echo command see in this script? Explain.
  • Redefine the IFS shell variable so that the output of the second echo is the same as the first.
share|improve this question
2  
I guess this is homework. Please show us what you've already found out, where you're stuck and what specifically you don't understand. We can't do all the work for you. –  slhck Apr 10 '12 at 20:53
    
Well, let's say this isn't homework.. If people are going to downvote, they should still explain. –  ekaj Apr 10 '12 at 20:58
2  
@K.Martin If you are stuck then I'd suggest going over your class notes and reading material again and come back to it after. If we just gave you the answer it won't help you at all. –  Ben Richards Apr 10 '12 at 21:05
1  
@SebastianStumpf This question, as it stands, should be nowhere on the Stack Exchange network. –  slhck Apr 10 '12 at 21:08
1  
@K.Martin You haven't bothered to even attempt to solve this problem yourself. Edit your question, show us what you already know, even if it's just a book, you can look for information online (e.g. about this "IFS") or Bash arguments. We can help you then, but unless you don't help yourself, you won't learn anything. –  slhck Apr 10 '12 at 21:20
show 15 more comments

closed as not a real question by slhck, Simon Sheehan, Ben Richards, Diogo, Dave M Apr 10 '12 at 21:56

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

Welll... You could have easily found out yourself if you wrote set -x before the other commands e.g.:

$ cat test.sh
#!/bin/bash
set -x

twoliner="This is line 1.
This is line 2."
echo "$twoliner"
echo $twoliner

If you would have executed the script with set -x, you would have noticed what actually happens:

$ ./test.sh 
+ twoliner='This is line 1.
This is line 2.'
+ echo 'This is line 1.
This is line 2.'
This is line 1.
This is line 2.
+ echo This is line 1. This is line 2.
This is line 1. This is line 2.

HTH

share|improve this answer
    
This is helpful, but it would be good to explain what the IFS has to do with this (and echo's quoted arguments). See here: tldp.org/LDP/abs/html/internalvariables.html –  slhck Apr 10 '12 at 21:10
1  
This doesn't satisfy the question of why what actually happens does happen. It's just the same as plugging a problem into a calculator. Sure, you can get an answer, but you don't get the benefit of thinking about the process. Which is why I'm holding off from answering, even like this. –  Ben Richards Apr 10 '12 at 21:12
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.