PDA

View Full Version : bash syntax, space in path command



sselt
March 19th, 2013, 02:30 AM
$ pwd
$ ~/pathwith space

$ if [ $(pwd) = ~/pathwith space ]; then echo "true"; fi
$ bash: [: too many arguments

What is wrong? I've tried escaping the space and quotes around the path. :-k

Vaphell
March 19th, 2013, 05:52 AM
i will use echo instead of pwd

$ echo "~/pathwith space"
~/pathwith space
$ [ $(echo "~/pathwith space") = ~/pathwith space ] && echo true
bash: [: too many arguments
$ [ $(echo "~/pathwith space") = "~/pathwith space" ] && echo true
bash: [: too many arguments
$ [ "$(echo "~/pathwith space")" = "~/pathwith space" ] && echo true
true

comparison in [] requires exactly 2 arguments for = (4 total: 'arg1', '=', 'arg2', ']' )
when you have an expression that can be reduced to [ ~/pathwith space = ~pathwith space ], [ sees:
1. ~/pathwith
2. space
3. =
4. ~/pathwith
5. space
6. ]
you have to use quotes and/or escaping to make it crystal clear what is supposed to be a single 'word', even in case of embedded commands that give space ridden output.

to showcase it let's write a small function that will use the same stuff as [ but will print the parameters it gets.
Open new terminal window and type (it will locally override the stock [ command)

$ [() { printf "%s\n" "$@"; }
$ [ $( echo "a b" ) = a b ]
a
b
=
a
b
]
$ [ $( echo "a b" ) = "a b" ]
a
b
=
a b
]
$ [ "$( echo "a b" )" = "a b" ]
a b
=
a b
]

kevdog
March 19th, 2013, 06:08 AM
Try this

if [ "`pwd`" = "~/pathwith space" ]; then echo "true"; fi

Bucky Ball
March 19th, 2013, 06:40 AM
Thread moved to Programming Talk.

sisco311
March 19th, 2013, 07:18 AM
@Vaphell and kevdog

Tilde expansion only applies when `~' is unquoted. ;) See BashPitfalls 26 (link in my signature).


@OP: Check out BashFAQ 031 and 020 (link in my sig) and http://mywiki.wooledge.org/Quotes.

I'd use the new test command:

if [[ "$(pwd)" == "$HOME/dir name" ]]
then
...
fi

Vaphell
March 19th, 2013, 08:13 AM
i know about ~ but too many arguments doesn't sound like a problem with tilde expansion (more like lacking in-depth knowledge how the parsing in bash works) so i ignored it.
Once the OP deals with it he can move to fixing possible issues with "~/dir" vs ~/dir vs /home/username/dir

sselt
March 19th, 2013, 08:52 AM
Thanks @Vaphell, @kevdog, and @sisco311. I learned something today.

What was throwing me off a previous script, with no spaces, that works.

When in doubt, just quote the whole damn thing!

Haha. I know that's not true, but it might make a funny signature,
which other people would then bash. har har.

schragge
March 19th, 2013, 10:41 AM
Just nitpicking, sorry.


I'd use the new test command:

if [[ "$(pwd)" == "$HOME/dir name" ]] The first pair of double quotes may be omitted here: the new form of test ([[...]]) takes care of quoting the results of parameter expansion and command substitution (this was a big point behind introducing it into the shell in the first place):

[[ $(echo a b) == 'a b' ]] && echo ok works the same as
[ "$(echo a b)" = 'a b' ] && echo ok