Search code examples
bashshellenvironment-variablesheredocsshpass

How to access environmental variables inside sshpass interactive mode


The below shell script snippet prints the environmental variable outside of sshpass, but not inside.

#!/bin/bash

[[ $1 = '' ]] && BRANCH="develop" || BRANCH=$1

echo $DESTINATION_FOLDER // prints from env

sshpass -p $PASSWORD ssh -o StrictHostKeyChecking=no $SERVER <<-'ENDSSH'

    echo $DESTINATION_FOLDER // doesn't print anything, output: empty

    exit
ENDSSH

Am I missing any option to be passed to the interactive mode with sshpass so that it can read environmental variables defined in the local system?


Solution

  • I suggest to replace 'ENDSSH' with ENDSSH to allow bash to interpret the variable in your here-document.

    See: https://en.wikipedia.org/wiki/Here_document#Unix_shells