Piodo
Piodo

Reputation: 616

Bash script - pass output from source script to variable

I got the bash starter script, that source unique tools from some env file. Then I proceed to run python script that will use those.

output=$(source $envPath 2>&1) give me text output in output variable, but in the future when I finally run python script (from that bash script) I do not have access to sourced tools from .env file.

It works fine for single source $envPath, the python script got access, but I cant read the output of that source.

output=""
# source $envPath >$output # doesnt work
# source $envPath | $output # doesnt work
echo $output

I need the output to verify it and execute correct action

Upvotes: 0

Views: 735

Answers (2)

choroba
choroba

Reputation: 241748

Save the output to a temp file, from which you can populate a variable:

1.sh

#!/bin/bash
tmp=$(mktemp)

. 2.sh > "$tmp"
output=$(< "$tmp")
echo "$output"
echo "$EXPORTED"

2.sh

echo 123
EXPORTED=1

Output of 1.sh:

123
1

Upvotes: 1

guidot
guidot

Reputation: 5333

This question is not python-related in my opinion, but a pure shell-syntax issue.

export output=`source $envpath`

should do.

Upvotes: 1

Related Questions