0

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

2
  • Can you call it twice? Commented Sep 18, 2018 at 9:29
  • 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 Commented Sep 18, 2018 at 9:44

2 Answers 2

1

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

export output=`source $envpath`

should do.

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

2 Comments

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: 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.
1

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

1 Comment

I thought about solution like this. As far I wont find better solution, I will use this as workaround. Thanks

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.