This question already has an answer here:
Using bash, my
pwd contains directories with spaces:
$ pwd /a/b c/d
$ ls "$PWD" bar foo # yay!
It also works if I put that code inside a script. Now if I split that command in two:
$ cat foo echo \"$PWD\" $ cat bar ls $(./foo)
foo generates the quoted path:
$ ./foo "a/b c/d" # ok
bar is not treating the quotes the way I'd expect:
$ ./bar ls: "/a/b: No such file or directory ls: c/d": No such file or directory # boo!
This is not academic. The command I actually want to split up is horrendous. Rather than having it in a single script, I want to break it into two scripts: one that generates the properly quoted parameters and another that is drastically simplified (and calls that first one). It so happens that this horrendous list of parameters is useful for multiple commands so that's another reason to have it in a separate script.
update 1: this works BTW:
$ cat foo ARGS="$PWD" $ cat bar source ./foo ls "$ARGS" $ ./bar bar foo
But it seems hokey to put the args into a variable.
update 2: @user454038's solution works for a single space in a directory name but breaks at two spaces.
update 3: @mikeserv and @user454038 educated me sufficiently that I got this working (for one and two, and presumably more, spaces in directory names):
$ cat foo echo "$PWD" $ cat bar ls "$(./foo)"
There we go! Thanks all!