I want to write a script that opens a terminal with different tabs, logon to a server using ssh in each tab and execute a command in each tab. For example: htop.

The below script opens a terminal with 4 tabs all logged in on the server. But if I try to add the htop command in the cmd= line it does not work anymore...


cmd="bash -c 'ssh user@host';bash"

for i in 1 2 3 4; do
      foo+=($tab -e "$cmd")         

gnome-terminal "${foo[@]}"

exit 0

I have tried this...

cmd="bash -c 'ssh user@host htop';bash"

...because the ssh --help says that the syntax for ssh is:

usage: ssh [user@]hostname [command]


In its simplest form:

ssh -t user@host "command; bash"

-t is the critical part here. It forces the host to allocate a virtual terminal to the process, which allows it to stay open.

If you just want to run htop on a load of servers, you can omit the bash at the end but that does mean if you quit htop, you'll drop back to a local terminal.

  • I tried cmd="bash -c 'ssh -t user@host htop';bash". It works but if I quit htop I drop back to local terminal and I don't want this happens – Red Jul 10 '13 at 10:13
  • 1
    @Red Check your quotes - they're all over the place - you need to group htop and bash together so they're both run on the remote server. bash -c 'ssh -t user@host "htop;bash"' would be better but I don't see why you're trying to fry this thing in a bash wrapper in the first place. It isn't required - you're only running one command locally. – Oli Jul 10 '13 at 10:14
  • Thank you, now it works with cmd="ssh -t user@host htop;bash" – Red Jul 10 '13 at 11:03
  • A good answer, but there must be something better to use than bash in order not to terminate. – mc0e Oct 31 '14 at 14:59
  • @mc0e Depends what you want to do. This question is more about getting a useful shell after running your command. If you just want to pause, you could substitute bash for read. – Oli Oct 31 '14 at 15:14

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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