$foo (meaning the first
$ in my echo command is dropped)
$ is run as a command. Echo doesn't require spaces between arguments, it just takes them.
$'$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
$$ it echos the process ID.
pwd (which means the bash expands
$foo, my variable, to its value)
"" 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.
$~/scripts (I expected it to print
~/scripts and not
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
So first it echos
$ then it expands
`pwd` which is run and returns
~/Scripts. But we had the
$ print earlier - so the whole output is