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
2
  • I've updated the question. Prompt is still missing. Commented Aug 23, 2017 at 11:44
  • If you just want to Print the line, then use echo to print the line and then use read to read value. Its a workaround. Commented Aug 23, 2017 at 11:47

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.