What is wrong? I've tried escaping the space and quotes around the path.Code:$ 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.Code:$ pwd $ ~/pathwith space $ if [ $(pwd) = ~/pathwith space ]; then echo "true"; fi $ bash: [: too many arguments
Last edited by sselt; March 19th, 2013 at 08:55 AM.
i will use echo instead of pwd
comparison in [] requires exactly 2 arguments for = (4 total: 'arg1', '=', 'arg2', ']' )Code:$ 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
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)
Code:$ [() { 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 ]
Last edited by Vaphell; March 19th, 2013 at 06:09 AM.
if your question is answered, mark the thread as [SOLVED]. Thx.
To post code or command output, use [code] tags.
Check your bash script here // BashFAQ // BashPitfalls
Try this
if [ "`pwd`" = "~/pathwith space" ]; then echo "true"; fi
Thread moved to Programming Talk.
@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:
Code:if [[ "$(pwd)" == "$HOME/dir name" ]] then ... fi
Last edited by sisco311; March 19th, 2013 at 07:21 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
Last edited by Vaphell; March 19th, 2013 at 08:16 AM.
if your question is answered, mark the thread as [SOLVED]. Thx.
To post code or command output, use [code] tags.
Check your bash script here // BashFAQ // BashPitfalls
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.
Just nitpicking, sorry.
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):
works the same asCode:[[ $(echo a b) == 'a b' ]] && echo okCode:[ "$(echo a b)" = 'a b' ] && echo ok
Last edited by schragge; March 19th, 2013 at 10:47 AM.
Bookmarks