Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having difficulties figuring out some the nuances between single and double quotes within a variable context

I define:

foo=pwd

then run these:

echo $'$foo'

Which echos $foo (meaning the first $ in my echo command is dropped)

echo $"$foo"

This echos pwd (which means the bash expands $foo, my variable, to its value)

echo $`$foo`

Finally this echos $~/scripts (I expected it to print ~/scripts and not $~/scripts)

can somebody help me figure out these differences?

share|improve this question

Ultimately, the last one prints $~/scripts because $`foo` is a combination of a literal $ followed by a command substitution; so the leading $ is interpreted as a literal $ and the trailing `$foo` as a command substitution.

share|improve this answer
    
I removed the code tags and replaces with just ```````` characters etc. Feel free to rollback if you prefer the code tags – Tim Feb 18 at 12:53
    
@Tim Thanks, not sure what I did exactly but for some reason after your edited the post appeared just as disrupted as in my first revision, so I rolled it back... And now I see it looks ok in the revision. Not sure what happened, gonna test this in the formatting sandbox before rolling back to your version since I fear I might disrupt the post again. Regardless, thanks for the effort. – kos Feb 18 at 13:08
    
sure, no issues. Mine uses zero-width spaces, so be careful as you edit. – Tim Feb 18 at 13:09

First case:

echo $'$foo'

Which echos $foo (meaning the first $ in my echo command is dropped)

The first $ is run as a command. Echo doesn't require spaces between arguments, it just takes them.

So echo $'$foo' expands to run the command $ (which isn't a command so it ignores it) and then echo '$foo'. Anything in '' is taken at face value - it doesn't expand it or anything. You can put what you want in '' and nothing happens.

If you replace the $ with $$ it echos the process ID.


Second:

echo $"$foo"

This echos pwd (which means the bash expands $foo, my variable, to its value)

That's because "" doesn't prevent it expanding. "" is normally used to ensure there is a value to compare to.

If you have a variable that could be empty (e.g. it is from a grep result) then you want to compare to another value / variable, you have to put the first variable in "". This ensures that there is always a comparison to check, rather than just an empty section of code (as bash sees) which gives an error.


Finally, number 3. This is interesting.

echo $`$foo`

This echos $~/scripts (I expected it to print ~/scripts and not $~/scripts)

That's because (in this case) echo is printing the $ character first. Putting something in backticks causes the value of the variable to be run as a command (the better way to do it in more recent versions of bash is $(command))

So first it echos $ then it expands ​`$foo`​ to ​`pwd`​ which is run and returns ~/Scripts. But we had the $ print earlier - so the whole output is $~/Scripts.

share|improve this answer
    
Nice work on some very difficult formatting! – Arronical Feb 18 at 12:42
1  
@Arronical Thanks :) You can't see it but there are some zero-width spaces there for the last sentence. The things I do for imaginary internet points...! – Tim Feb 18 at 12:43
1  
+1 Good answer. I would just add that quoting variables should be the default unless you have a specific reason not to - not just to handle possible null values. See unix.stackexchange.com/questions/171346/… for more than you ever wanted to know about this. ;) – Joe Feb 25 at 8:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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