Change a readonly environment variable in Bash [duplicate] - bash

This question already has answers here:
Unset readonly variable in bash
(16 answers)
Closed 3 years ago.
If I accidentally set a readonly variable like this:
declare -r VAR="foo"
When I should have set it to bar.
How can I set it to bar?
Thanks

declare, as any other statement that modifies a variable within a shell, only does so within the current environment. As the shell does not somehow save its environment on eixst and load it again when restarted, you can just close your shell and open a new one and you'lll have your initial environment back.
It's actually way harder to make changers persist in a shell than to reset them...

Related

Python3: equivalent of "export VARNAME" in terminal [duplicate]

This question already has answers here:
How to set environment variables in Python?
(19 answers)
Reading and writing environment variables in Python? [duplicate]
(4 answers)
set environment variable in python script
(3 answers)
Closed 5 years ago.
In terminal (bash) on OSX I can set an environment variable, using the syntax export VARNAME=1 or export VARNAME="hello"; and that persist as long as the session is running, or until the terminal window is closed.
What would be the equivalent form, to do the same via Python3? I would like to avoid to call Popen just to set a global variable.
Also I need this variable only for the purpose to run my python code; once the script is done, I do not need it anymore; so even if it last only for the lifespan of my script running, it is acceptable.

bash export not setting variable [duplicate]

This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 3 years ago.
I have a simple environment setup script that exports some environment variables like so.
#!/bin/sh
export NEWROOT=~/some/directory
echo $NEWROOT
This echos the correct directory, but after its run, when I echo $NEWROOT in the same shell, it returns nothing.
Any idea why the variable isn't setting?
The shell is run in a separate process, and environment variables in a child process do not affect the environment variables in the parent process.
If you want to run the script in the same process, you can use the dot command, like this:
. myscript
A child process can't affect the environment variables of its parent. If you source the script instead, that will evaluate the script in the current environment, leaving NEWROOT.

Bash - force error if variable not defined [duplicate]

This question already has answers here:
How can I make bash treat undefined variables as errors?
(2 answers)
Closed 6 years ago.
I work with a lot of shell scripts that use bash variables. So, for example, I might have a script like this:
option1="-blah_blah"
option2="-yada_yada"
option3="-whatever"
...
option99="-something_else"
./myCommand "$option1 $option12 $option97 $option45"
I am constantly editing that last command to run various engineering tests. The problem is, sometimes I misspell a variable. In that case, Bash simply substitutes an empty string, and my command does the wrong thing silently.
Is there a way to have Bash throw an exception when I try to use a variable that is not defined?
Use:
set -e # Stop on error. I can't believe that this is not default.
set -u # Stop if trying to use un-initialized variables.

Shell script variable reflection [duplicate]

This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Closed 8 years ago.
I am writing a shell script (#!/bin/sh) which has a variable VAR which contains the name of another variable FOO, which in turn is set to BAR.
FOO=BAR
VAR=FOO
I want to get the value of the variable named in VAR, so something like:
echo "${$VAR}"
But that does not seem to work. Suggestions?
In Bash:
echo "${!VAR}"
Without Bash (though it works in Bash too):
eval echo "\${$VAR}"
Beware: eval is a very general mechanism that can run you into problems very easily. It works fine here, but be cautious about using it more generally.

Set variable from script [duplicate]

This question already has an answer here:
Best way to set environment variables in calling shell
(1 answer)
Closed 8 years ago.
I have a script "set_var.sh" written like this
#!/bin/bash
export NAME=release
export ROOT=/Volumes/name/dev/release
but if I run this set_var.sh from terminal, afterward I issue set command to check variables I could not find NAME and ROOT var be set.
I am wondering what is wrong in my case.
it was set in sub-shell.
you need
source set_var.sh
If you simply run set_var.sh, it runs in its own shell which exits, losing the variables that were set.
If you want to change variables in your interactive shell, you can use:
source set_var.sh
or the shorthand,
. set_var.sh
This will execute the lines of the script as if they were typed into your interactive shell.
Note that when you "source" a file this way, it does not require the "shebang" on the first line.
Note also that this is feature exists in Bourne shell as well, but only in the short-form version.

Resources