2

Using bash, my pwd contains directories with spaces:

$ pwd
/a/b c/d

This works:

$ 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

but 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!

marked as duplicate by mikeserv, Scott, cuonglm bash Dec 18 '15 at 7:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

The issue is as @mikeserv pointed out, the quotes. Excess quotes. The solution is to not have quotes in your foo in the first place:

dir structure (and we have cd to the d dir):

~
└── a
    └── b c
        └── d

Within d dir, we have already foo and bar you posted. But now create:

foo2:

echo $PWD

bar2:

set -x
ls "$(./foo2)"
  • we have set -x so you can see clearly what is going on

When you run /bar in command prompt, you see:

$ ./bar2
+++ ./foo2
++ ls '/home/youruser/a/b c/d'
bar  bar2  foo  foo2

Just remove the set -x from the code, to successfully get the desired output:

bar  bar2  foo  foo2

Explanation

If you add set -x to your original bar, and run, you will see

$ ./bar
+++ ./foo
++ ls '"/home/youruser/a/b c/d"'
ls: cannot access "/home/youruser/a/b c/d": No such file or directory

This is the same as if on command prompt you ran:

$ ls '"/home/youruser/a/b c/d"'
ls: cannot access "/home/youruser/a/b c/d": No such file or directory
  • The culprit is the extra " quote
  • which was introduced in your original foo
  • as soon as " is removed, ls works

Thus, removing quotes in your original foo, as we have done with foo2, ensures ls receives it properly without excessive quotes and it then works.

  • That works! Thanks for adding the educational set -x too. – Bill Burcham Dec 18 '15 at 3:45
  • You're welcome, glad it helped. – clarity123 Dec 18 '15 at 3:57
  • 1
    now try it for a $PWD with 2 consecutive spaces - like /a/b ​ c/d (but dont copy paste that example - type it) – mikeserv Dec 18 '15 at 4:13
  • @mikeserv two consecutive spaces in a directory doesn't work. d'oh. Sorry user454038, I un-solved this question. – Bill Burcham Dec 18 '15 at 15:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.