How should I write ‘/users/johndoe/dropbox/’ to refer to in a shell script? ‘~/dropbox/’ gives an error.
That should work in general, but sometimes if the path will not be expanded for the tilde (~) if it is in quotes.
This can never work, because you’ve turned it into a string.
So – the shell cannot see
~/ and expand it properly.
Quoting is only necessary if you have spaces in your path, so this will work appropriately:
Here’s an example that would need escaping and how to escape it:
Quoting the whole tilde-path string is a very novice mistake I’ve made myself in years gone by.
On the “spaces in the path” point presumably they work if you precede each one by a backslash.
Yes, generally, but then you have to figure out which other characters might need backslashing. Generally it is best to use single quotes if you want to avoid all interpolation, and double quotes if you want to include a variable. Backslashes work, but rapidly leads to leaning tooth pick syndrome.
So my tendency - in the few scripts where it has been needed - is to break a cd into two parts: Quotable and non-quotable.
Escaping spaces works perfectly fine, but it is disparagingly referred to as the leaning-toothpick method – because it’s labor intensive and hard to read.
I made a macro to make this easier. (Make sure to see the second one in the thread as well.)