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.
Related
This question already has answers here:
Difference between sh and Bash
(11 answers)
$BASH_VERSION reports old version of bash on macOS, is this a problem that should be fixed?
(4 answers)
Closed 10 months ago.
I have some code that looks like this:
export AWS_REGION='us-east-1'
export CLUSTER_NAME='my_cluster'
export PROJECT_NAME='my_project'
source ./utils.sh
...additional functions...
annotate_cluster # comes from utils.sh
annotate_cluster, which comes from utils.sh, relies on the environment variable PROJECT_NAME. However, when I run it, it complains _utils.sh: line 60: FOO-${PROJECT_NAME^^}: bad substitution. Why can it not access the environment variable I have set?
This question already has answers here:
Dynamic variable names in Bash
(19 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Bash - variable variables [duplicate]
(4 answers)
Closed 2 years ago.
I recently started bash scripting and got stuck with a very basic usecase, searched stackoverflow/google but couldn't find a way to achieve what I am trying to do.
I have a script color.sh
#!/bin/bash
Apple="Red"
Orange="Orange"
Banana="Yello"
echo $$1
What I am trying to achieve is print the color of fruit and accept fruit from command line. The output I want is
./color.sh Apple -> Red, but what I get is some random number which I think is process Id.
This question already has answers here:
How to install a bash function containing variables using a bash script? [duplicate]
(2 answers)
Create a script that adds lines of code to .bashrc then reloads the terminal
(2 answers)
Closed 4 years ago.
I've written a very simple utility to allow a couple of colleagues to easily access some system logs.
The dependencies for this are installed by running a curl to an install.sh file on GitHub.
There are a couple of functions and aliases that are handy to have in your bash profile. Out of interest how would I programmatically add items to a user's bash profile in a shell script.
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...
This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Reference an appended variable?
(3 answers)
Closed 5 years ago.
How can I access an environment variable from another? I have the following in my shell
#!/usr/bin/env bash
set -x
export A_version=1.0.0
component=A
echo ${${component}_version}}
the bash script after the run gives me
temp.sh: line 9: ${${component}_version}}: bad substitution
You can use eval to do this. Here is a working version of your script that prints 1.0.0:
export A_version=1.0.0
component=A
eval "echo \$${component}_version"
For more information, see this page:
http://tldp.org/LDP/abs/html/ivr.html
Update: A safer way to do the same thing in Bash would be:
export A_version=1.0.0
component=A
var=${component}_version; echo "${!var}"
Note that you have to run this script with bash, not sh.