Search code examples
unixsshkshenv

Use remote server env variable in ksh via an SSH command


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.


Solution

  • 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'