passing arguments to remote: quoting problem? - SSH

This is a discussion on passing arguments to remote: quoting problem? - SSH ; I try to issue a remote with the command line ssh "$host" "$what" "$@" in a shell script. But as soon as one argument contains a whitespace, ssh passes it splitted to the remote program: [14:39] denkbrett:chamelon% ssh home.schottelius.org ls ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: passing arguments to remote: quoting problem?

  1. passing arguments to remote: quoting problem?

    I try to issue a remote with the command line

    ssh "$host" "$what" "$@"

    in a shell script. But as soon as one argument contains a
    whitespace, ssh passes it splitted to the remote program:

    [14:39] denkbrett:chamelon% ssh home.schottelius.org ls -l "a b"
    ls: a: No such file or directory
    ls: b: No such file or directory

    [14:39] denkbrett:chamelon% ssh home.schottelius.org
    [14:40] creme:~% ls -l "a b"
    -rw-r--r-- 1 nico nico 0 2007-07-10 14:38 a b

    Am I doing something wrong or is that a feature or even a bug
    in ssh?

    The original posting is available on
    http://home.schottelius.org/~nico/un...nding_spaces-1

    Greetings

    Nico

    --
    Think about Free and Open Source Software (FOSS).
    http://nico.schottelius.org/document...the-term-foss/

    PGP: BFE4 C736 ABE5 406F 8F42 F7CF B8BE F92A 9885 188C

  2. Re: passing arguments to remote: quoting problem?

    Nico Schottelius wrote:
    > I try to issue a remote with the command line
    >
    > ssh "$host" "$what" "$@"
    >
    > in a shell script. But as soon as one argument contains a
    > whitespace, ssh passes it splitted to the remote program:


    > [14:39] denkbrett:chamelon% ssh home.schottelius.org ls -l "a b"
    > ls: a: No such file or directory
    > ls: b: No such file or directory


    There are two shells in your example, the local one (running the ssh
    client), and the remote one (executing the 'ls'). They are both going
    to be doing tokenizing. So the local one uses the quotes to create a
    token of ab, but that string is transmitted without quotes and
    the remote shell turns it into two tokens for the 'ls'.

    When possible, I usually find it easiest to enclose the entire remote
    command within single quotes for the local shell, allowing any other
    quoting to be retained for the remote shell.

    local% ssh remote 'ls -l "a b"'

    --
    Darren Dunham ddunham@taos.com
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >

  3. Re: passing arguments to remote: quoting problem?

    Darren Dunham writes:

    >Nico Schottelius wrote:
    >> I try to issue a remote with the command line
    >>
    >> ssh "$host" "$what" "$@"
    >>
    >> in a shell script. But as soon as one argument contains a
    >> whitespace, ssh passes it splitted to the remote program:


    >> [14:39] denkbrett:chamelon% ssh home.schottelius.org ls -l "a b"
    >> ls: a: No such file or directory
    >> ls: b: No such file or directory


    >There are two shells in your example, the local one (running the ssh
    >client), and the remote one (executing the 'ls').


    Well, the local one is (as you've seen referenced in the url in the last message)
    a shell script. So the local one expands correctly and passed "a b" as one
    parameter to ssh.

    >They are both going
    >to be doing tokenizing. So the local one uses the quotes to create a
    >token of ab, but that string is transmitted without quotes and
    >the remote shell turns it into two tokens for the 'ls'.


    Well, there are no quotes to pass within execv() and friends (which I
    assume ssh will call).

    But it seems that the problem may be caused by the shell and not ssh:

    Regarding to http://www.opengroup.org/onlinepubs/...lities/sh.html
    a program could call 'sh -c cmd arg1 arg2' (which should be still together,
    as ssh does no expanding.

    But testing with a dash, csh, zsh and a bash on my system I do not get the
    expected results:

    sh -c "ls" "ls" should produce the error that there is no file ls, but
    it displays the command line.

    Regarding to the manual of bash, the first positional parmater is assigned
    to $0, so I even tried

    sh -c "ls" "ls" "ls"

    which still does not result in an error (which I would like to have).

    I'm gonna investigate some time to find out, whether the shells behave
    incorrectly or whether I'm interpretating the manuals wrong.

    Nico
    --
    Think about Free and Open Source Software (FOSS).
    http://nico.schottelius.org/document...the-term-foss/

    PGP: BFE4 C736 ABE5 406F 8F42 F7CF B8BE F92A 9885 188C

  4. Re: passing arguments to remote: quoting problem?

    Nico Schottelius wrote:
    > sh -c "ls" "ls" should produce the error that there is no file ls, but
    > it displays the command line.


    No, it shouldn't. `sh -c a b' sets $0 to be 'b', and then executes
    the command 'a'. If 'a' doesn't reference $0, then the 'b' is
    effectively ignored. So `sh -c ls ls' executes the first 'ls' as a
    command, having put the second 'ls' into $0 where it isn't
    referenced.

    sh -c "ls ls", on the other hand, executes the command `ls ls'.
    which does indeed report `ls: ls: No such file or directory'.
    --
    Simon Tatham "The distinction between the enlightened and the
    terminally confused is only apparent to the latter."

+ Reply to Thread