0
As the title says, I want to modify environment variables on a remote server using 'ssh'.
My first attempt was:
$ ssh mysvr -l sikaiwei "getId=$ORANGE; echo $ORANGE; ORANGE=$((ORANGE+1)); echo $ORANGE; echo 'getId is '+$getId;"
At first, I didn't define $ORANGE on the server host, but defined $ORANGE as "1" on the client, and I got:
1
1
getId is +
Note that the command is picking up the client's value of $ORANGE,
and the update (ORANGE+1
) isn't happening.
Then I removed the $ORANGE definition on the client host,
and defined $ORANGE on the server by putting a command into my .bash_profile
.
But
$ ssh mysvr -l sikaiwei "getId=$ORANGE; echo $ORANGE; ORANGE=$((ORANGE+1)); echo $ORANGE; echo 'getId is '+$getId;"
resulted in
getId is +
It can't print the value of $ORANGE that I attempted to initialize on the server host! Then I tried this:
$ ssh mysvr -l sikaiwei ". .profile; getId=$ORANGE; echo $ORANGE; ORANGE=$((ORANGE+1)); echo $ORANGE; echo 'getId is '+$getId;" getId is +
It still doesn't work.
I tried using '..' (single quotes) instead ".." (double quotes) as you said, but it prints nothing...
$ ssh myth@mysvr 'echo orangeis $ORANGE'
orangeis
I want to print
getId=1; echo 1; ORANGE=2; echo 2; …
instead of
getId=; echo ; ORANGE=1; echo ; …
So how can I use the $ORANGE value from the server host instead of the client value?
My server machine runs Windows, but I installed Cygwin. My client machine runs Linux.
Edit: I found part of the problem: I was setting $ORANGE in ~/.bash_profile
on the server host, and I can get value of $ORANGE using "echo $ORANGE" in an interactive shell (Cygwin), but this wasn't affecting ssh
command strings.
For example, I get nothing when my client host uses
$ ssh sikaiwei@mysvr 'echo orange is $ORANGE'
orange is
Like that, I don't get the value of $ORANGE that I do in a session on the server.
The . .profile
that I added didn't help.
But if I
$ ssh sikaiwei@mysvr '. ~/.bash_profile;echo orange is $ORANGE'
orange is 4
I got the value!
Oh, did I forget to mention: I want these variables on the server to be persistent.
In other words, if I execute an ssh
command that changes the value of $ORANGE from 4 to 42, then I want $ORANGE to be 42 when I do another ssh
command.
You say, “I … defined $ORANGE on the server host.” How? What did you do to define ORANGE on the server? – Scott – 2013-12-19T23:44:03.763
Server machine is mine.It run Windows,but installed Cygwin.I definde ORANGE in .bash_profile on the Server. – orange – 2013-12-20T01:15:51.843
Are you sure
.bash_profile
gets processed forssh
jobs? Can you check by puttingecho $ORANGE
into.bash_profile
? – Scott – 2013-12-20T01:48:04.183My fault...First I use
. ~/.bash_profile
,then I can use the variable defined in~/.bash_profile
– orange – 2013-12-20T08:35:35.393Well, the bash(1) man page says that
– Scott – 2013-12-20T22:30:14.407bash
reads.bash_profile
only if it is an interactive shell, or if it is invoked with the--login
option. If you invokessh
with a command string argument, the shell will not be considered interactive, and so.bash_profile
will not be read automatically.