2

I have an environment variable like

ABC_XYZ=false

When I echo $ABC_XYZ It gives me false Now I am inside a script I do these steps

>echo $ABC_XYZ 
false
>ABC_XYZ=true
>echo $ABC_XYZ
true

Now after exiting from the script echo $ABC_XYZ again gives me false Any way through which i can set the value as true from inside the script.

Thanks in advance.

0

2 Answers 2

5

You will have to run the script in the shell's current context using source.

Assuming your script is in your current directory:

$ source ./script_filename

or

$ . ./script_filename

Quoting from help source:

source: source filename [arguments] Execute commands from a file in the current shell.

Read and execute commands from FILENAME in the current shell.  The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.

To make the variable available in subsequently forked child processes(either shell or any other process), you'll need to export the environment variable like this in the script:

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

2 Comments

Added source ~/.bash_profile and export ABC_XYZ still no success
source script must be written on the command line and not inside the script
1

Try to run the below script file with this:

source ./export.bash

2 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
okay thanks for advice.i will do from next time. I am too new to stack overflow.

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.