Shell – For a same unix or linux user, different sets of environment variables

aliasenvironment-variableskey-authenticationshelltcsh

I'm using tcsh, and for a specific project every member of my team connects to a server with the same user. (This is something we cannot change).

The situation arises because I want to have some custom environment variables and aliases, and to do so I have my own .tcshrc file (namely .tcshrc_cust) which I load as the first action when connecting to that machine:

source .tcshrc_cust

and even though this works pretty well, I'm experiencing a problem when using vim: if I get to the shell from inside vim (with :sh) I fall into a normal shell, without my custom env. vars. and aliases.

Is there a solution for this, besides using a different user in that machine?


SOLUTION (given by @Shawn):

I prefixed my key into .ssh/authorized_keys with:

command="setenv subuser noz; tcsh"

and wrote at the end of .tcshrc file these lines:

if ($?subuser) then
    source .tcshrc_$subuser;
endif

And everything works fine now.

Best Answer

You can make .tcshrc check a special environment variable (like subuser), and conditionally source .tcshrc_cust. When you log in, run subuser=nozimica tcsh. It will get that enironment variable and execute your custom rc script. In addition, vim's :sh command will work. You can even make it fancy and source .tcshrc_$subuser; that way everyone could do it.

You can skip the part where you run subuser=nozimica tcsh when you login by having ssh run it for you. If you setup ssh key authentication, then in ~/.ssh/authorized_keys on the server, you can prefix your key with command="subuser=nozimica tcsh", and ssh will run that command for you.