4

I have a script on my server named test.sh:

#!/bin/bash
read -p "Select an option [1-4]: " option
echo "You have selected $option"

When I run it through ssh manually, I see this:

me@me:~$ ssh root@server
root@server's password:
[...]
root@server:~# bash test.sh
Select an option [1-4]: 48
You have selected 48

When I run it as ssh remote command, I see this:

me@me:~$ ssh root@server 'bash test.sh'
root@server's password: 
48
You have selected 48

I am unsatisfied with this output because it's missing Select an option [1-4]: prompt string and the original script which from has I derived test.sh contains a lot of interactive dialogue strings like this and I need them all.

I know that read prints it's prompt to stderr so I tried to start the script with following commands in case if stderr is omitted, but the output stays still the same:

ssh root@server 'bash test.sh >&2'
ssh root@server 'bash test.sh' >&2
ssh root@server 'bash test.sh 2>&1'
ssh root@server 'bash test.sh' 2>&1

Why this is happening and how to make ssh remote command work as expected?

UPD

I have changed the test.sh to this:

#!/bin/bash
echo Connected
read -p "Select an option [1-4]: " option
echo "You have selected $option"

but the output still missing the prompt string:

me@me:~$ ssh root@server 'bash test.sh'
root@server's password: 
Connected
66
You have selected 66
user619271
  • 4,766
  • 5
  • 30
  • 35

1 Answers1

5

You need to use -t option in ssh to assign a pseudo-terminal to ssh session:

ssh -q -t root@server 'bash test.sh'
anubhava
  • 761,203
  • 64
  • 569
  • 643