0

I have a bash script bash-one.sh that calls bash-two.sh inside of it. I want bash-two.sh to export some string once it's done running (the string is depending on some stuff that happens inside of the script) so that the bash-one.sh script that's running it can use it later

I was trying to have bash-two.sh echo $BLAH but that doesnt work. I also tried having it export $BLAH with no luck either...

Naji
  • 674
  • 2
  • 14
  • 35
  • 2
    it sounds like you are looking for the syntax `output=$(bash-two.sh)` -- https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash?rq=1 – Rorschach Nov 01 '19 at 00:09
  • @picklerick hmm. not sure if i'm following.. if `bash-one` assigns `bash-two.sh` to `output`, what should be written in `bash-two.sh` to properly export the variable i need once it's done running? – Naji Nov 01 '19 at 00:18
  • whatever `bash-two` puts on stdout will be assigned to `output`, see [command substitution](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html) in the manual – Rorschach Nov 01 '19 at 00:20
  • @picklerick ah i see. I don't want the entire stdout though as the file is outputting a lot of things, i simply want one variable to be exported by the time the script is done executing so i can reference it in `bash-one`.. is that possible? – Naji Nov 01 '19 at 00:22
  • 2
    If you execute `bash-two.sh` with the `source` command, it will be executed in the same shell process as `bash-one.sh`, so all its variable assignments will be visible. – Barmar Nov 01 '19 at 00:24
  • There's no way for a child process to set variables directly in a parent process. Exporting only happens in one direction, from parent to child. – Barmar Nov 01 '19 at 00:24
  • @Naji : Define what you mean by "exporting". Creating a string and using it in the calling process, could be done by passing it via stdout, as pickle rick suggested, or by writing it to a file and then reading the file in the parent process. There are other possibilities too (named pipe, shared memory, sockets), but this is more complicated to set up and probably an overkill for your application. – user1934428 Nov 01 '19 at 07:43

2 Answers2

0

Thanks @Barmar for the easy solution! I simply sourced bash-two.sh to be able to access the variable i needed

ie.

bash-one.sh:

#!/bin/bash

source ./bash-two.sh $1
echo "${TEST_VARIABLE}"

bash-two.sh:

#!/bin/bash

TEST_VARIABLE=$1 

"There's no way for a child process to set variables directly in a parent process. Exporting only happens in one direction, from parent to child. " -Barmar

Naji
  • 674
  • 2
  • 14
  • 35
0

There is also another way that you can accomplish this task.

In your bash2 script you can apply output redirection like bellow:

exec 3>&1 1>/dev/tty

This commnand when placed in the beginning of the script will save the position of stdout in file descriptor 3, and will force stdout (fd 1) to be printed on the screen.

You can then echo from script2 the variables that you want to fd3, like this:

echo "$BLAH" >&3

In this way you can capture from script1 the output of script2 using

testvar="$(bash-two.sh)"

Test:

cat sc1.sh
exec 3>&1 1>/dev/tty
echo "hello from sc1"           #By default will be printed to >&1, redirected to /dev/tty=screen 
sc1var="this comes from sc1"
echo "$sc1var" >&3

localvar=$(bash sc1.sh)
hello from sc1            #forced to be printed on /dev/tty

echo "$localvar"
this comes from sc1
George Vasiliou
  • 6,130
  • 2
  • 20
  • 27