0

I can't find any answer to this "easy looking" problem.

I would like to execute an ssh command using a ksh shell or script which use an env variable of the SERVER.

Example: ssh user@server "ls $DIR"

Where $DIR is an env variable define on the server (in this case: a directory path) and not the $DIR define on my client env.

In worst case scenario I can use something like env | grep DIR | cut -d "=" -f 2 to get the var but it looks weird.

Thanks for any help.

Community
  • 1
  • 1
Schiltech
  • 3
  • 2

2 Answers2

0

The SSH command execution shell is a non-interactive shell, whereas your normal shell is either a login shell or an interactive shell.

In fact not all environment varialbles are available in non-interactive shell, you need to check ksh manual to figure out the configuration files that ksh reads when running in non-interactive mode, in case of bash those are roughly following

/etc/profile
~/.bash_profile
~/.bash_login
~/.profile

Just find out the corresponding ones for ksh and move/copy your DIR environment variable definition to there on sever side.

deimus
  • 9,565
  • 12
  • 63
  • 107
  • Thanks for clarification. I have my variable in my .profile use by ksh on server side. But ssh still use the definition of the variable present client side (DIR is define on both environnement with different values). How can I make ssh understand I want to use the definition of the server ? – Schiltech Jan 09 '15 at 14:41
  • Ok I'm good now. By lauching my .profile at ssh session start and using Kenster answer syntax, I'm now using my remote variable. – Schiltech Jan 09 '15 at 15:32
0
ssh user@server "ls $DIR"

Double-quoted strings undergo variable interpolation. So "$DIR" is being replaced on the local system, then the shell invokes the ssh command with the resulting string.

To pass the literal command through to the remote system, use single quotes:

ssh user@server 'ls $DIR'
Kenster
  • 23,465
  • 21
  • 80
  • 106