Your shell script is executed by another shell which is a sub process.
There is no direct way to pass a variable from the child process to its parent.
One method could be to execute the script with your current shell with the dot (".") operator, but any call to exit will stop your current shell and you will inherit all the variables of your script :
. ./create_backup.sh && ln -s $BACKUP_FILE /folder/here
A better way is to use file to store your variable.
At some point in your script you save the value of the variable :
declare -p BACKUP_FILE > /tmp/somefile.sh
or :
echo BACKUP_FILE=\""$BACKUP_FILE"\" > /tmp/somefile.sh
Then you can execute :
if ./create_backup.sh
then . /tmp/somefile.sh
ln -s $BACKUP_FILE /folder/here
fi
Another way without files, could be to read from the stdin of the script :
OUTSCRIPT=` ./create_backup.sh | grep 'BACKUP_FILE=' `
Here is an another solution : the parenthesis "()" creates a sub-shell to protect you from the exit calls and unwanted variables of the script AND you don't have to modify the script :
if ( . ./create_backup.sh && declare -p BACKUP_FILE > /tmp/somefile )
then . /tmp/somefile
ln -s $BACKUP_FILE /folder/here
fi
create_backup.sh
? Is that your script? Why don't you just modify it to print only the name to stdout? – terdon♦ Dec 22 '16 at 15:24