Relative path in shell script

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.

See Execute a Shell Script action (KM Wiki) and
How To Quote Paths by @ccstone

Hey @ALYB,


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:

~/'My Folder/'

Quoting the whole tilde-path string is a very novice mistake I’ve made myself in years gone by.



1 Like

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.

Hey Martin,

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.)

Single-Quote a POSIX Path String or Tilde-Based POSIX Path String