2

I am running a remote putty session from windows to my Red Hat Enterprise Linux server.

Due to company restrictions, I am unable to change my Linux user's default shell from bash to zsh. I instead have read only access to ~./bash_profile, which executes ~/.bashrc (where I also have only read only access), which in turn executes ~/.bash_custom where I have write access. Inside ~/.bash_custom, I execute zsh to change my shell.

Including the line to execute zsh prevents the execution of any remote command provided by putty. I know this is the problem since if I comment out my execution of zsh it works fine.

I have several aliases and PATH modifications inside my zsh which I'd like to use as part of the remote command executed from putty. How can I pass the remote command from the bash shell to zsh for it to be executed there?

7
  • 1
    What Linux are you using? Sounds like it is one of the ones that have .profile source .bashrc because normally, .bashrc wouldn't even be read when sshing into a machine. Do you have code in your ~/.profile that makes it source .bashrc? Also, are you opening an interactive shell session or are you just trying to run a single command?
    – terdon
    Commented Nov 21, 2023 at 16:02
  • @terdon Bash reads .bashrc when invoked non-interactively over rsh or ssh (with some variation between versions and distributions, IIRC making ssh like rsh was originally a Debian patch). Commented Nov 21, 2023 at 17:09
  • I'm not sure what you're doing exactly, but this looks self-inflicted. Why would you exec zsh in .bashrc? I can't think of a good reason to do that. Why not do it in .bash_profile, so that it's only done in interactive shells? Commented Nov 21, 2023 at 17:10
  • @Gilles'SO-stopbeingevil' yes, non interactively only, that's why I asked if the OP is opening an interactive session or trying to run a single command. If not, if this is opening an interactive login shell, then they must have a profile that reads bashrc.
    – terdon
    Commented Nov 21, 2023 at 17:14
  • @terdon I am using Red Hat Enterprise Linux. I have read only access to ~/.bash_profile which then executes ~/.bashrc which I also have only read only access to which then executed ~/.bash_custom which I have write access to and which is where I run zsh Commented Nov 22, 2023 at 9:29

1 Answer 1

1

On some (most these days) systems, bash is configured at build time so that when run over ssh, bash reads ~/.bashrc (and corresponding system-wide file in /etc) even when run non-interactively like when running ssh some command where sshd runs bash -c 'some command'¹.

What you could do is add at the top of your ~/.bash_custom:

export SHELL="$(command -v zsh)" # tell everyone your preferred shell
case ${SSH_CONNECTION+s}:${-//[^ilc]}:$#:$SHLVL in
  (s:c:0:1) exec zsh -c -- "$BASH_EXECUTION_STRING";;
  (*:i:0:*) exec zsh;;
esac

That executes zsh with the same code when bash is run as bash -c 'some code' by sshd, and zsh alone when bash is run interactively.

You could limit that second case to cases where bash is invoked as a login shell only with:

if shopt -q login_shell; then
  exec zsh
fi

and/or only via ssh by replacing *:i:0:* with s:i:0:*


¹ Strictly speaking, it should run bash -c -- 'some command' but since there are shells where that's not supported (or not needed like in csh, fish, rc...), it doesn't.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .