Skip to main content
Tweeted twitter.com/StackUnix/status/1007943552683397121
added 59 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

I suddenly realized I don't know how to execute things over SSH.

I tried to do

$ ssh user@server sh -c 'echo "hello"'

but it outputs nothing, or rather, it outputs an empty line. If the command given to ssh is run through $SHELL -c on the remote host, then I can see why that is (or I can try to rationalize it to myself anyway).

Ok, second try:

$ ssh user@server 'echo "hello"'
hello

All well and good.

Now for what I was really hoping would work:

$ ssh user@server 'echo "hello $1"' sh "world"
hello  sh world

Hmm... where does the sh come from? This is indicating that $1 is empty and that what really gets executed on the other side is something like

$SHELL -c 'echo "hello  sh world"'

and not what I had hoped,

$SHELL -c 'echo "hello $1"' sh "world"

Is there a way to safely pass arguments to the script executed via ssh, in a sane and sensible way that is analogous to running

sh -c 'script text' sh "my arg1" "my arg2" "my arg3" ...

but on the remote host?

My login shell, both locally and remotely is /bin/sh.


Safely = Preserving whitespace etc. in arguments.

Sanely = No crazy escaping of quotes.

I suddenly realized I don't know how to execute things over SSH.

I tried to do

$ ssh user@server sh -c 'echo "hello"'

but it outputs nothing, or rather, it outputs an empty line. If the command given to ssh is run through $SHELL -c on the remote host, then I can see why that is (or I can try to rationalize it to myself anyway).

Ok, second try:

$ ssh user@server 'echo "hello"'
hello

All well and good.

Now for what I was really hoping would work:

$ ssh user@server 'echo "hello $1"' sh "world"
hello  sh world

Hmm... where does the sh come from? This is indicating that $1 is empty and that what really gets executed on the other side is something like

$SHELL -c 'echo "hello  sh world"'

and not what I had hoped,

$SHELL -c 'echo "hello $1"' sh "world"

Is there a way to safely pass arguments to the script executed via ssh, in a sane and sensible way that is analogous to running

sh -c 'script text' sh "my arg1" "my arg2" "my arg3" ...

but on the remote host?


Safely = Preserving whitespace etc. in arguments.

Sanely = No crazy escaping of quotes.

I suddenly realized I don't know how to execute things over SSH.

I tried to do

$ ssh user@server sh -c 'echo "hello"'

but it outputs nothing, or rather, it outputs an empty line. If the command given to ssh is run through $SHELL -c on the remote host, then I can see why that is (or I can try to rationalize it to myself anyway).

Ok, second try:

$ ssh user@server 'echo "hello"'
hello

All well and good.

Now for what I was really hoping would work:

$ ssh user@server 'echo "hello $1"' sh "world"
hello  sh world

Hmm... where does the sh come from? This is indicating that $1 is empty and that what really gets executed on the other side is something like

$SHELL -c 'echo "hello  sh world"'

and not what I had hoped,

$SHELL -c 'echo "hello $1"' sh "world"

Is there a way to safely pass arguments to the script executed via ssh, in a sane and sensible way that is analogous to running

sh -c 'script text' sh "my arg1" "my arg2" "my arg3" ...

but on the remote host?

My login shell, both locally and remotely is /bin/sh.


Safely = Preserving whitespace etc. in arguments.

Sanely = No crazy escaping of quotes.

added 15 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

I suddenly realized I don't know how to execute things over SSH.

I tried to do

$ ssh user@server sh -c 'echo "hello"'

but it outputs nothing, or rather, it outputs an empty line. If the command given to ssh is run through $SHELL -c on the remote host, then I can see why that is (or I can try to rationalize it to myself anyway).

Ok, second try:

$ ssh user@server 'echo "hello"'
hello

All well and good.

Now for what I was really hoping would work:

$ ssh user@server 'echo "hello $1"' sh "world"
hello  sh world

Hmm... where does the sh come from? This is indicating that $1 is empty and that what really gets executed on the other side is something like

$SHELL -c 'echo "hello  sh world"'

and not what I had hoped,

$SHELL -c 'echo "hello $1"' sh "world"

Is there a way to safely pass arguments to the script executed via ssh, in a sane and sensible way that is analogous to running

sh -c 'script text' sh arg1"my arg2arg1" arg3"my arg2" "my arg3" ...

but on the remote host?


Safely = Preserving whitespace etc. in arguments.

Sanely = No crazy escaping of quotes.

I suddenly realized I don't know how to execute things over SSH.

I tried to do

$ ssh user@server sh -c 'echo "hello"'

but it outputs nothing, or rather, it outputs an empty line. If the command given to ssh is run through $SHELL -c on the remote host, then I can see why that is (or I can try to rationalize it to myself anyway).

Ok, second try:

$ ssh user@server 'echo "hello"'
hello

All well and good.

Now for what I was really hoping would work:

$ ssh user@server 'echo "hello $1"' sh "world"
hello  sh world

Hmm... where does the sh come from? This is indicating that $1 is empty and that what really gets executed on the other side is something like

$SHELL -c 'echo "hello  sh world"'

and not what I had hoped,

$SHELL -c 'echo "hello $1"' sh "world"

Is there a way to safely pass arguments to the script executed via ssh, in a sane and sensible way that is analogous to running

sh -c 'script text' sh arg1 arg2 arg3 ...

but on the remote host?

I suddenly realized I don't know how to execute things over SSH.

I tried to do

$ ssh user@server sh -c 'echo "hello"'

but it outputs nothing, or rather, it outputs an empty line. If the command given to ssh is run through $SHELL -c on the remote host, then I can see why that is (or I can try to rationalize it to myself anyway).

Ok, second try:

$ ssh user@server 'echo "hello"'
hello

All well and good.

Now for what I was really hoping would work:

$ ssh user@server 'echo "hello $1"' sh "world"
hello  sh world

Hmm... where does the sh come from? This is indicating that $1 is empty and that what really gets executed on the other side is something like

$SHELL -c 'echo "hello  sh world"'

and not what I had hoped,

$SHELL -c 'echo "hello $1"' sh "world"

Is there a way to safely pass arguments to the script executed via ssh, in a sane and sensible way that is analogous to running

sh -c 'script text' sh "my arg1" "my arg2" "my arg3" ...

but on the remote host?


Safely = Preserving whitespace etc. in arguments.

Sanely = No crazy escaping of quotes.

Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

Executing `sh -c` script through SSH (passing arguments safely and sanely)

I suddenly realized I don't know how to execute things over SSH.

I tried to do

$ ssh user@server sh -c 'echo "hello"'

but it outputs nothing, or rather, it outputs an empty line. If the command given to ssh is run through $SHELL -c on the remote host, then I can see why that is (or I can try to rationalize it to myself anyway).

Ok, second try:

$ ssh user@server 'echo "hello"'
hello

All well and good.

Now for what I was really hoping would work:

$ ssh user@server 'echo "hello $1"' sh "world"
hello  sh world

Hmm... where does the sh come from? This is indicating that $1 is empty and that what really gets executed on the other side is something like

$SHELL -c 'echo "hello  sh world"'

and not what I had hoped,

$SHELL -c 'echo "hello $1"' sh "world"

Is there a way to safely pass arguments to the script executed via ssh, in a sane and sensible way that is analogous to running

sh -c 'script text' sh arg1 arg2 arg3 ...

but on the remote host?