Nickolay Kondratyev
Nickolay Kondratyev

Reputation: 5211

How to use specific shell during pseudo-tty allocation?

I want to be able to execute remote command in bash instead of default zsh on remote machine (preferable without having to modify settings on remote machine)

Example:

ssh -t -t some-command-that-only-works-in-bash-to-execute-remotely

Upvotes: 0

Views: 1091

Answers (2)

JayRugMan
JayRugMan

Reputation: 148

As mentioned in previous comments, here-document can be utilized as such:

me@mycomp ~ $ ssh [email protected] "/bin/sh <<% 
echo 'user\np@55word!' > ~/cf1
%"
[email protected]'s password: 
me@mycomp ~ $ cat ~/cf1
user
p@55word!

From the man page

The following redirection is often called a “here-document”.

       [n]<< delimiter
             here-doc-text ...
       delimiter

 All the text on successive lines up
 to the delimiter is saved away and
 made available to the command on
 standard input, or file descriptor n
 if it is specified.  If the delimiter
 as specified on the initial line is
 quoted, then the here-doc-text is
 treated literally, otherwise the text
 is subjected to parameter expansion,
 command substitution, and arithmetic
 expansion (as described in the sec‐
 tion on “Expansions”).  If the opera‐
 tor is “<<-” instead of “<<”, then
 leading tabs in the here-doc-text are
 stripped.

Upvotes: 0

Fred
Fred

Reputation: 6995

You can do this :

ssh user@host "bash -c \"some-command-that-only-works-in-bash-to-execute-remotely\""

Please be careful with quoting, however. The arguments to your ssh command will first undergo local expansions and word splitting, then be passed to ssh, to then be submitted to the remote shell and undergo a second round of (remote) expansion and word splitting.

For instance, this will echo the local value of LOGNAME :

ssh user@host "bash -c \"echo $LOGNAME\""

This will echo the remote value of LOGNAME :

ssh user@host "bash -c \"echo \$LOGNAME\""
ssh user@host 'bash -c "echo $LOGNAME"'

If you want to understand why, try replacing ssh with echo and see what command the remote end would receive.

You can also do this :

echo "some-command-that-only-works-in-bash" | ssh user@host bash
ssh user@host bash < <(echo "some-command-that-only-works-in-bash")

You could issue multiple commands with this method (one line each) and they would all be executed remotely. Piping the output of a function designed to issue several commands is useful once in a while, as is redirecting a local script so that it can be executed on the remote machine without having to be copied.

Upvotes: 1

Related Questions