Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
BASH script to set environment variables not working
I have written the following script to set some environment variables when needed:
Below the command and the results I can see on my terminal: the script runs, but the variables are not set at the end.
What is wrong?
- environment-variables
- 1 Shells are opened hierarchically. You can set a parent's shell's vars using . ./yourscript.sh – Janac Meena Commented Jan 29, 2020 at 14:54
3 Answers 3
export exports the variable assignment to child processes of the shell in which the export command was ran. Your command-line environment is the parent of the script's shell, so it does not see the variable assignment.
You can use the . (or source ) bash command to execute the script commands in the current shell environment and achieve what you want, e.g.
Will produce
The source command, often seen in scripts, is a bash synonym for . , which is part of the POSIX standard (so . is available in dash, for example, but source isn't).
( . script.sh and source script.sh will first look for script.sh in PATH , so it's safer to specify the path to script.sh .)
- 37 You don't need export to pass variables on to subshells, a subshell is a copy of your current shell, including variables and functions etc. Exported variables gets copied to new processes spawned from the shell, regardless of that process being another shell or not. Secondly, . is the POSIX command for sourcing. Bash adds source as a more readable synonym for it, but you can't rely on it being available in sh. Lastly . ./script instead of . script if you want to avoid surprises. mywiki.wooledge.org/BashFAQ/060 – geirha Commented Jul 18, 2011 at 5:55
- 1 Note that if you source a script AND use pipe then the sourced environment is not available to parent. e.g. 'source setit.sh' is okay. 'source setit.sh |tee setit.log' is not okay. Surprising. Not intuitive. – gaoithe Commented Sep 8, 2016 at 11:47
- 1 Just learned the hard way that if you are in the habit of using set -euo pipefail in all your scripts if you source that script you will set it in the current terminal which means the terminal will exit when it receives a non-zero exit code (ie a failure). Not the best experience. What you can do is call set +euo pipefail at the end of the script to turn it back off but still have the safety of failures exiting your script. – Mike Harrison Commented Jul 30, 2022 at 1:48
- Yes. But it will not show with command > env – Joeri Commented Mar 2, 2023 at 15:41
When you run a script, it runs in a subshell. Variables are only valid within the context of that subshell. Set them in your .bashrc or .profile and read up on variables and subshells . The export statement works down hierachy (current shell and all it's subshells) not up as in your example.
Alternatively (if you really want the script to effect the enviroment of your current shell) run it as:
That causes it to run in your current shell but will not pass variables up the hierarchy either.
I often want to set an environment variable without hassle.
Here is what I add to my .bashrc to implement this convenience.
You must log in to answer this question.
Not the answer you're looking for browse other questions tagged bash environment-variables ..
- The Overflow Blog
- This developer tool is 40 years old: can it be improved?
- Can a programming language implement time travel?
- Featured on Meta
- The December 2024 Community Asks Sprint has been moved to March 2025 (and...
- Stack Overflow Jobs is expanding to more countries
Hot Network Questions
- Are these two circuits equivalent? How to prove it?
- How to merge at intersection line point?
- Can a 4-d creature twist your right hand into a left hand without breaking it?
- A mistake in revised version
- A SAT question about SAT property
- Is this blade too pitted?
- Systemd service to start only after CIFS mount
- Counting in Latin
- Will Homescreen website shortcuts be gone if I switch carriers?
- Equality test of two hyperbolic expressions
- Are linear mixed effects model robust to unbalanced clusters?
- What is the meaning of "meanwhile" in this context?
- Movie ends with wall mounted alien hand moving. Poison lump on hand
- PSE Advent Calendar 2024 (Day 21): Wrap-Up
- Two gang cover - half receptacle half flat?
- Can I omit 'мы' if the verb ends with '-ем'?
- Makefile for a tiny C++ project
- Will a PC complain if a USB 2 flash drive is powered externally?
- How was Lemech allowed to hunt?
- How is it conceptually possible to have fault-tolerant quantum computing with constant overhead
- Frege on truth?
- How to read this old French speed gauge?
- PSE Advent Calendar 2024 (Day 23): 2089
- I need to visualize the movement of a solar sail?
UNIX for Beginners Questions & Answers
Using read to assign value to bash variable not working.
10 More Discussions You Might Find Interesting
1. shell programming and scripting, do while loop + read from file + assign line to a variable, discussion started by: parththakkar, 2. unix for advanced & expert users, how to read a text file and assign the values in the same to a variable in loop, discussion started by: manishab00, 3. shell programming and scripting, bash assign string to variable, discussion started by: lio123, 4. shell programming and scripting, how to write script read file and assign it as variable, discussion started by: proghack, 5. shell programming and scripting, read the csv file and assign the values in to variable, discussion started by: rajbal, 6. shell programming and scripting, read a file and assign the values to a variable, discussion started by: depakjan, 7. shell programming and scripting, assign bash command to variable, discussion started by: moxy, 8. shell programming and scripting, read and assign each character from the string to a variable, discussion started by: tek-e, 9. unix for dummies questions & answers, how to read a config file and assign to variable, discussion started by: redlotus72, 10. shell programming and scripting, assign value to variable is not working, discussion started by: agustincm, member badges and information modal.
assign value to variable is not working
echo > aux
Whenever you pipe things into a loop, that loop becomes its own process, with it's own seperate process space -- which inherits variables you already have, but things it sets don't propogate back.
You can write the variable into a file in the loop
After assigning the value 0 to your variable, you break the while loop so you never display the variable with the new value 0.
Whith KSH the modification of variable inside loops aren't lost (that is not the case with bourne shell). Try the following script:
[CODE]var=init ls | while read file do var="Last file is $file" done
Jean-Pierre.
IMAGES
COMMENTS
Apr 6, 2018 · That declares the variable nextcount with the integer attribute, which causes arithmetic evaluation to be performed on it automatically whenever it is assigned. (Run help declare in bash or look here for details.) This is not a standard POSIX shell feature, but bash supports it. The assignment does not need to immediately follow the declare command
Any string containing = (in any position other than at the beginning of the string) which is not a redirection and in which the portion of the string before the = is a valid variable name is a variable assignment, while any string that is not a redirection or a variable assignment is a command. In STR = "foo", STR is not a variable assignment.
Mar 7, 2022 · The sh language is not the C language, you can't just expect one thing that works in one language will work in another language. sh is before all a command line interpreter. Most things are done by commands which the shell is there to invoke.
Apr 15, 2022 · When you run a script, it runs in a subshell. Variables are only valid within the context of that subshell. Set them in your .bashrc or .profile and read up on variables and subshells. The export statement works down hierachy (current shell and all it's subshells) not up as in your example.
Oct 12, 2006 · After assigning the value 0 to your variable, you break the while loop so you never display the variable with the new value 0. Whith KSH the modification of variable inside loops aren't lost (that is not the case with bourne shell).
Jun 29, 2018 · Commands on either side of a pipe are executed in separate processes, so variable setting on one won't affect the other. Please provide an example with more context so that we can see what you're trying to accomplish.
Hi, I am attempting to assign the output of the following command, to two bash variables, var1 and var2 using "read," but it doesn't seem to be working. Code: | The UNIX and Linux Forums
Dec 14, 2010 · Hi Rohon, I intend to use the stored variable in my shell script later ( $a ) set a=`awk -F "/" '{print $NF}' bp | awk '{print $1}'` Thanks.
Oct 12, 2006 · Unix Linux Community assign value to variable is not working. Shell Programming and Scripting. agustincm October 12, 2006, 12:39pm 1. Hi . The next script campares ...
Nov 4, 2015 · Shell script (assign to variable) not working [duplicate] Ask Question Asked 9 years, 1 month ago. Modified 9 years, 1 month ago. Viewed 1k times