0

I have a use case similar to the below code. Need to export parameterized variables/identifier to remote shell script from a shell script. I used the below code but I cannot export the value. Please suggest how to do it.

A.sh (script 1)

#!/bin/bash
sshpass -p asdf ssh [email protected]<<'ENDSSH'
export directory="$1"
sh /../B.sh
ENDSSH

B.sh (script 2)

#!/bin/bash
echo directory=$directory
mkdir $directory

#Execution
sh A.sh '/data/2017-7-7/' 

#output
directory=

I get the value in the remote shell script when I hard code the value.

export directory='/data/2017-7-7/' 

I want to export parameterized variable, please suggest how to implement this.Thanks

1 Answer 1

1

Just remove quotes from the first ENDSSH:

sshpass -p asdf ssh [email protected] << ENDSSH
export directory="$1"
sh /../B.sh
ENDSSH

According to the bash manual:

Here Documents

[...]

The format of here-documents is:

[n]<<[-]word
    here-document
delimiter

No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

[...]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.