Results 1 to 8 of 8

Thread: bash syntax, space in path command

  1. #1
    Join Date
    Mar 2012
    Beans
    11

    bash syntax, space in path command

    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.
    Last edited by sselt; March 19th, 2013 at 08:55 AM.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Need bash syntax help with space in path command

    i will use echo instead of pwd
    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
    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)
    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

  3. #3
    Join Date
    Mar 2007
    Location
    Denver, CO
    Beans
    7,958
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Need bash syntax help with space in path command

    Try this

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

  4. #4
    Join Date
    Feb 2008
    Location
    Land of fire and drought
    Beans
    Hidden!
    Distro
    Xubuntu

    Re: Need bash syntax help with space in path command

    Thread moved to Programming Talk.

  5. #5
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Need bash syntax help with space in path command

    @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.

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Need bash syntax help with space in path command

    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

  7. #7
    Join Date
    Mar 2012
    Beans
    11

    Re: Need bash syntax help with space in path command

    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.

  8. #8
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Need bash syntax help with space in path command

    Just nitpicking, sorry.
    Quote Originally Posted by sisco311 View Post
    I'd use the new test command:
    Code:
    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):
    Code:
    [[ $(echo a b) == 'a b' ]] && echo ok
    works the same as
    Code:
    [ "$(echo a b)" = 'a b' ] && echo ok
    Last edited by schragge; March 19th, 2013 at 10:47 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •