• You have ssh access to a server
  • You do not know which shell you have (and that the devil changes it next time you log in)
  • You want the command foo to be run every time you log in (even non-interactively)
  • You want the environment variable bar set to quux every time you login (even non-interactively)

In particular you can not assume:

  • The operating system
  • That shells other than /bin/sh are available

http://mywiki.wooledge.org/DotFiles (Remote non login non interactive shells) and Can 'ssh <host> <command>' be configured to always load server-side startup files? cover this nicely for Bash, but seem to ignore that the devil might change your login shell to something other than Bash.

What are the similar techniques for ksh, bash, ash, dash, csh, tcsh, zsh? (Spawning /bin/sh from the login shell is considered cheating by the devil, and is thus not an acceptable answer).

  • I assume the devil will not give you a command= limitation in your remote authorized_keys, though? Commented Dec 3, 2017 at 17:39
  • 1
    A very boring devil would change your login shell to /bin/false.
    – ilkkachu
    Commented Dec 3, 2017 at 17:56
  • (1) If you cannot assume what the target operating system is, you cannot assume that it even has environment variables.  (2) Likewise, how can you assume that foo exists?  If the target is an off-the-shelf / out-of-the-box Windows system, good luck running ps, uname, or even ls -l.  (3) As I read this question, I had a sense of déjà vu.  Then I saw your name, and recognized it as the author of 'switch' based on shell, which seems pretty similar. Commented Dec 4, 2017 at 0:05
  • @G-Man Yep, similar, but different: Here you are allowed to save stuff into startup-files.
    – Ole Tange
    Commented Dec 4, 2017 at 6:54
  • The more time I spend on this page, the more confused I get about what the question is, and whether it is answerable.  What if the devil changes your login shell to qwertysh (which you’ve never heard of), which reads .qwertysh_profile and .qwertyshrc (and nothing else)?  Are you asking a pie-in-the-sky question about any shell on any operating system, or are you asking a maybe-real-world question about ksh, bash, ash, dash, csh, tcsh, and zsh (and fish, and any other well-known, widely-distributed shell) on any well-known, widely-distributed Unix / Linux operating system? Commented Dec 4, 2017 at 18:41

1 Answer 1


You could try something like this:


Match User ed
    ForceCommand "bash -c ";export THING=a;/bin/bash -i""

This is suboptimal, and I'm sure it can be golf'd, but I'll leave that as an exercise for the reader.

  • (1) Is the ; at the beginning of the command necessary?  Why?  (2) The question explicitly mentions non-interactive logins.  It looks like your answer explicitly fails that requirement.  (3) The question was modified after you posted this to say “you can not assume … that shells other than /bin/sh are available”, so you missed that moving target ex post facto.  … … … … … … … … … … … … … … … … … … … … … …  Don’t feel bad; I’m not sure that the question is answerable, as written. Commented Dec 4, 2017 at 18:48

You must log in to answer this question.

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