I'm writing a script to pass a complete file path with spaces in the name to a remote computer over ssh, which needs to run using iTerm. It needs to happen through AppleScript embedded into a shell script so I can take control of iTerm, create new split views, etc.
I've simplified my script here purely to the section that isn't working. For demonstration purposes, and so people here might be able to try alternatives, I've changed the command in question to the 'cd' command, as the error is the same (it can't find the path due to spaces in it).
#!/bin/bash
PATH="$1"
ssh server@192.168.50.4 "osascript \
-e 'tell application \"iTerm\"' \
-e 'activate' \
-e 'set newWindow to (create window with default profile)' \
-e 'tell first session of current tab of current window' \
-e 'write text \"cd "$PATH"\"' \
-e 'end tell' \
-e 'end tell'"
This works fine if there's no spaces in the path. The remote machine opens up an iTerm window and the path is changed to cd path. However, if there's spaces in the name, such as…
Volumes/Work/My Folder With Spaces
…iTerm responds with -bash: cd: Volumes/Work/My: No such file or directory
I've tried using $@ and putting extra quotes around the $1 variable and/or the $PATH variable. Also, I know there's an AppleScript command that's something like "quoted form of (POSIX path)" but I have no idea how to integrate it here within the bash script.