I would like to ssh to a machine with a pre-defined setting, specifically in zsh.

To do so, I have tried doing the following from the client (inspired by this answer):

$ ssh -t machine@domain "STARTUP_OPTION='A'; exec /path/to/zsh;"

and then I have the following .zshrc in the server:

if [ "$STARTUP_OPTION" = "A" ]; then
    echo "Logging in with A"

This doesn't seem to work. How can I login with a specific shell while passing the value for a shell variable in the remote shell?


2 Answers 2


By default the only environment variable that's transmitted over an SSH connection is TERM. You can pack information there but you've got to be sure that it'll be unpacked on the server side. The client can transmit other messages, but the server needs to be set up to accept them with an AcceptEnv directive in /etc/sshd_config. Under Debian and most derivatives (Ubuntu, Mint, …) all variables whose name begins with LC_ are also accepted by the SSH server. These variables are conventionally used for locale settings, but you can use one of your own to pass something else. If your server allows it, you can simply write


Another possibility is to run a command based on the user authentication key (this requires only control of ~/.ssh/authorized_keys, not the sshd configuration).

If you aren't able to transmit environment variables, you can set it on the command line and then execute an interactive shell, as you've been doing. Since you're executing a new shell (that's necessary because you can't both specify an initial command to run and get an interactive shell), this needs to be an environment variable, not a shell variable.

ssh -t machine@domain 'export STARTUP_OPTION="A"; exec /path/to/zsh'

Yet another approach is to feed some data to the shell via its standard input, and then redirect the standard input to the terminal.

ssh -t localhost 'echo "echo foo; exec </dev/tty" | exec zsh -i'
stdin: is not a tty
stty: standard input: Inappropriate ioctl for device

Despite the error messages about not being able to initialize the terminal, zsh seems to cope properly with the interactive part of the session.

  • Gilles, should not it be /etc/ssh/sshd_config?
    – heemayl
    Oct 7, 2015 at 3:03
  • @heemayl Either, depending on the distribution. Oct 7, 2015 at 3:11

You could export the variable definition, so that the variable definition is inherited by the zsh process:

$ export foo=bar
$ exec zsh
$ echo $foo

Or use env:

$ exec env foo=bar zsh
$ echo $foo           

You must log in to answer this question.

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