Inserting a path in a shell script with a Keyboard Maestro variable doesn't work

Hi there,

Can anyone suggest or tell me why the following works fine:

But using a KM variable does not work:

From the KM wiki:

If you are passing a file/folder path in a Keyboard Maestro Variable, then it is best to put the Variable reference in double quotes so that spaces in the path will work, like this:

cat "$KMVAR_File_Path"

The second one fails for the same reason that the shell commands in the Terminal:

path='/Volumes/GoogleDrive/My\ Drive'
cd $path

fails. It fails because the \ is in the variable, and is no longer useful as a quoting. Meanwhile, the space in the variable is still useful for word breaking. So it gets it wrong in both ways, it is effectively equivalent to:

cd '/Volumes/GoogleDrive/My\\' 'Drive'

What you nee is:

  • Set variable test to /Volumes/GoogleDrive/My Drive
  • Execute Shell script cd "$KMVAR_test" && touch test.text

And note that as discussed in many places, that will still not work if you try to use ~/ in the path. If you want to do that, then you would beed to expand the tilde explicitly:

  • Set variable test to ~/My Documents
  • Filter variable test with Expand Filter in Path
  • Execute Shell script cd "$KMVAR_test" && touch test.text

Thanks so much for the quick reply, this worked. I was scratching my head for ages over it yesterday.