Setting environmental variable in linux via ssh [migrated]

I am working on three different issues so I have cloned source code in three different directories.

1) /nobackup/vivek/dev1/<source_code>..
2) /nobackup/vivek/dev2/<source_code>..
3) /nobackup/vivek/dev3/<source_code>..

I have made few aliases in .bashrc file for few modules so that I can jump to them quickly. I have used environmental variable named as CDR whose value can be dev1, dev2 or dev3 depending on which issue and directory I am working.

Example of one of the alias from my .bashrc file

alias acl='cd /nobackup/vivek/$CDR/<source_code>../acl'

Right now I have to change the value of the environmental variable manually. For example , if I want to work on issue no. 3 then I have to export the environmental variable CDR to dev3.

export CDR=dev3. 

Because for issue no. 3, I should be under dev3 folder i.e. /nobackup/vivek/dev3/<source_code>..

Similary if I am working on issue no. 1 or 2 I have to export the environmental variable manually to dev1 or dev2

export CDR=dev1 or export CDR=dev2

I thought to set the environmental variable CDR at the time of ssh itself. I have tried below command to accomplish that

ssh -t [email protected] 'export CDR=dev1'
or
ssh -t [email protected] 'export CDR=dev2'

When I execute the above ssh commands, my connection gets terminated as soon as I enter the password.

[email protected]'s password:
Connection to 122.11.33.21 closed.

Can anyone please help me here. I am not sure why my connection getting terminated. Is there any other way by which I can set my env variable at the time of login itself.


Solution 1:

You want to start bash (or your preferred shell) - best as login shell, to ensure your standard profile, rc-file etc. are loaded, too:

ssh -t user@server "export var=VALUE ; bash -l"

Or as pointed out by @they, the shorter notation

ssh -t user@server "var=VALUE bash -l"

will do, too.