1

I need make something like

$ source /etc/environments # called in bash.sh script

Of course after script finished no changes apply to shell. I know this is tricky if because child process cant modify parent 'bash' process. But May be another way to do so?

0

2 Answers 2

3

You should use

source bash.sh

Then it runs in the original shell instead of a child process.

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

Comments

1

As you have observed yourself a child process cannot set persistent environment variables. One of the usual work around are writing something like this to stdout:

% cat my_script
#!/bin/bash
echo "export MY_VAR=1234"

And then used in a command substitution:

eval "$(./my_script)"

An example of such script is dircolors

7 Comments

@123 It will work with bash scripts, but nothing else.
I ment that my_script needs to be written in the same language as where it's sourced, and no $(...) is not bash only.
I meant not portable,not just bash, sorry. But this still doesn't make any sense to do,
@123 OP asks for a way for a child process to set environment variables in the parent shell. I assume he want a general solution that also works when the child process is machine code, perl, python, ... Unless you can show me a way to source a python script, I will hold at this method.
What i meant is, I can't see a reason to use an external process to set variables in the current shell. You can run python et al. inside a sourced script and have them output the values you want anyway.
|

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.