3 added 9 characters in body
source | link

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?

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() {
  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?

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?

2 added 163 characters in body
source | link

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() {
  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?

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() {
  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.

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() {
  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?

1
source | link

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() {
  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.