Putty: login, execute command/change environment variable, and do NOT close the session

The target server needs to be configured to accept setting environment variables for the 1st option to work. The second is actually working fine, the problem is that it is designed to mimic

ssh user@foo command

which will just connect, run command and exit. You can have it remain open by giving it command; bash but that won't work for setting your variables sice a new shell will be started after the variable has been set.

So, short of having root access to the server so that you can enable the setting of environmental variables, the only way I can think of for you to do this is to edit ~/.bashrc the server and define your PS1 there. Add this line to ~/.bashrc:

PS1="some stuff"

Now, every time you log into that server, the prompt will be set for you.


Another way to do this would be to use a different rcfile for your bash session. Create a new file with these lines:

source /etc/profile
source ~/.bashrc
PS1='some stuff'

Save it as, for example, ~/.myps1, then in your putty settings, set the command to run on the remote server to:

bash --rcfile ~/.myps1

This will open a new shell session o the remote server and read in the file above which first reads .bashrc and then sets PS1.


For those, that cannot modify sshd config for various reasons and/or have +2000 servers (and no access to mass-configuration tools or can't/don't want to change settings for other users), here's a solution I came up with:

enter image description here

In PuTTY load the desired session, go to Connection > SSH. In the "Data to send to the server" section, in "Remote command" field use:

env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...] bash

Example:

env -u PS1 PS1="[\u@\h]\\$ " bash

I unset the variable first, because it didn't work otherwise.