1

Let's say that I have this bash function where I firstly connect to a Remote Desktop and then I want to use a second argument while I am connected to the command line of the Remote Desktop (that's why I've used the single quotation marks. However, I am not sure how to surround the $2 argument.

my_func () {
ssh blah blah blah $1 'cd $2'
}

I am not sure about the way I should specify the second argument. I've tried to run this function in the local machine as: my_func a b, where b is a directory in the Remote Desktop, but I don't seem to have the expected output.

4

You'd need to quote $2 in a way suitable for the login shell of the remote user. For instance, assuming it's Bourne-like:

shquote() {
  LC_ALL=C awk -v q=\' '
    BEGIN{
      for (i=1; i<ARGC; i++) {
        gsub(q, q "\\" q q, ARGV[i])
        printf "%s ", q ARGV[i] q
      }
      print ""
    }' "$@"
}

my_func () {
  ssh blah blah blah "$1" "cd -- $(shquote "$2")"
}

For instance, shquote() would transform Stephane's dir into 'Stephane'\''s dir' so the cd -- 'Stephane'\''s dir' command line is interpreted by the remote Bourne-like shell as running cd with a Stephane's dir directory argument.

In any case running a shell on a remote host that just does cd is not going to achieve anything useful.

More reading (and more options, including for the cases where the login shell of the remote user is not Bourne-like) at How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?

  • Thanks for the answer, I'll definitely check it (cd is just an example for simplicity, it's not the actual purpose)! – thanasissdr Oct 6 '17 at 11:47
  • While my solution might work in the "standard" case, this solution is much more robust. My solution will for example fail if $2 contains '. – Valentin B. Oct 6 '17 at 11:48
1
my_func () {
ssh blah blah blah $1 'cd $2'
}

Do not use single quotes. Bash variables are not expanded when you use single quotes, try ssh blah blah blah $1 "cd $2" it should work. Make sure that changing single quotes to double quotes does not make bash expand other things in your command though, or you might get an unexpected result.

  • You're welcome, here is some documentation on shell variable expansion for further reading if you are interested. – Valentin B. Oct 6 '17 at 11:45
  • 1
    That won't work properly if $2 contains any character special to the remote shell (like space, ;, $, &...). – Stéphane Chazelas Oct 6 '17 at 11:46
  • Indeed it is not safe if used naively, but you could also escape the special characters directly in the input arguments right ? It depends on who will be on the user end eventually. – Valentin B. Oct 6 '17 at 11:51
1

what about:

my_func() {
    blah blah blah blah $1 "cd \"$2\""
}

this doesn't work for parameters containing ".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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