Bash script - pass output from source script to variable
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.
output=$(source $envPath 2>&1)
It works fine for single source $envPath
, the python script got access, but I cant read the output of that source.
source $envPath
output=""
# source $envPath >$output # doesnt work
# source $envPath | $output # doesnt work
echo $output
I need the output to verify it and execute correct action
I can but it can give different results depending if it has been sourced already before/ for the first time or gave error and I want to support all the cases differently
– Piodo
Sep 18 '18 at 9:44
2 Answers
2
This question is not python-related in my opinion, but a pure shell-syntax issue.
export output=`source $envpath`
should do.
Executes, but the same problem as with
output=$(source $envPath 2>&1)
. When it runs to python script the python does not have access to tools that should be sourced– Piodo
Sep 18 '18 at 10:04
output=$(source $envPath 2>&1)
@Piodo: I find this distiction (between different times, when tools are available and when not) not sufficiently covered in your question. If the shell script only runs, when the tools are no longer accessible an intermediate file may help. But simply running the script when the tools are still available and preserving the environment variable should also succeed.
– guidot
Sep 18 '18 at 10:30
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
:
1.sh
123
1
I thought about solution like this. As far I wont find better solution, I will use this as workaround. Thanks
– Piodo
Sep 18 '18 at 10:08
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Can you call it twice?
– choroba
Sep 18 '18 at 9:29