Exporting environmental variables bash - bash

I am trying to create a bash script that toggles the output everytime the script is run (for some functionality in polybar). The easiest way in my mind to do this is based on the value of an environmental variable, but it isn't working as expected.
This is the script that I have written to test, assume this script is run by something else,and has access to the env variable.
echo $STATE
if [[ $STATE == "public" ]]; then
echo foo
export STATE='private'
elif [[ $STATE == "private" ]]; then
echo bar
export STATE='public'
fi
echo $STATE
My desired output is
$ ./test.sh
public
foo
private
$ ./test.sh
private
bar
public
but the actual output is
$ ./test.sh
public
foo
private
$ ./test.sh
public
foo
private
It seems that the export call isn't persistent

Your script runs as a child of your current shell. Child processes cannot directly modify the environment of their parent. That includes environment variables, the current working directory, how signals are handled, etcetera. A process can only modify its environment and, implicitly, the environment of any processes it starts.
The export in your script works fine. But it only modifies the value for that script or any child processes (e.g., other scripts) it spawns. By definition it cannot change the value in its parent process.
Solution 1: Implement the script as a shell function. Since functions run in the context of the current shell they can modify its state including its env vars.
Solution 2: Store the STATE value somewhere like a dot file in the user's home directory.

Related

What is the exact names of three kinds of variable in shell script? (Global Environment vs Global vs Local)

I read a lot of explanation of kinds of variable in bash shell script.
But I still confused because people uses mixed terminology for the same thing.
From my knowledge there are three kinds of variable even though I don't know exact official term for that.
I will define the name of them like belows for the clear communication.
Global Environment Variable
This is variable exported so that the whole script can access and child shell also can access
Global Variable
This is variable that can access in whole script scope but not all child shell.
Local Variable
This is variable that can acess only in a function scope. This variable defined with "local" keyword
Some people distinguish Global or Local by the point of that it is exported or not. They call the "2. Global Variable" as "Local Variable"
But some people distinguish Global or Local by the point of that it is function scope or the whole script scope.
What is exact official terminoloies for these three kinds of variables?
And what is the exact distinction for these?
For example,
$ cat vartest.sh
#!/bin/bash
var2="global_var2"
echo $var1 # (1)
echo $var2 # (2)
$ var1="local_var1"
$ source ./vartest.sh
local_var1
global_var2
I think var1 in (1) and var2 in (2) are totally same kind of variable. I can't find any difference between them.
Is it any different between (1) and (2) under the hood?
Continuing from above(or below) comments because comment doesn't have formatting function.
$ var1="local variable"
$ echo $SHLVL # echo shell level
1
$ bash # initiate sub-shell
$ echo $SHLVL
2 # this tells you are in sub-shell
$ echo $var1 # try to access local var from parent shell
$ exit # exit from sub-shell
$ echo $SHVL # confirm you are back to parent shell
1
$ export var1 # export 'var1' to environmental(global) variable
$ bash # initiate sub-shell again
$ echo $SHLVL # confirm you are in sub-shell
2
$ echo $var1 # now you can access this variable
local variable
$ exit
Here is another example on working with local (shell) variable in a script.
# Create a script as follow.
$ cat test.sh
#!/bin/bash
echo "Shell level is $SHLVL"
echo "var2 is $var2"
-----
$ bash test.sh # Run the script
Shell level is 2 # $SHLVL is accessible in all-level because it's environmental/global variable.
var2 is # var2 is empty because it's not created yet.
$ var2="passing var" bash test.sh
Shell level is 2
var2 is passing var # shell(local) variable is passed to the sub-shell/script
In a shell, global variable is environmental variable accessible in all-level. Local variable is only accessible in a current shell. And you can export local variable to global/environmental variable. I hope this clarifies a bit.
I think you are confused between shell environment and scripting.
Try following links.
https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_02.html
https://tldp.org/LDP/abs/html/localvar.html

Variable is not getting exported [duplicate]

This question already has an answer here:
bash - export doesn't work
(1 answer)
Closed 7 years ago.
I am running the following simple code in a shell script , but it seems like it cant export the variable :
#!/bin/bash
echo -n "Enter AWS_ACCESS_KEY_ID: "
read aws_access_key
export AWS_ACCESS_KEY_ID=$aws_access_key
After that I take the input from the user ,but when I run echo $AWS_ACCESS_KEY_ID I get a blank value .
Run your script in the current shell by using:
source your-script # this runs your-script in the existing shell
...or, if using a POSIX shell...
. your-script # likewise; that space is intentional!
not
./your-script # this starts a new shell just for `your-script`; its variables
# are lost when it exits!
...if you want variables it sets to be available to the shell that calls it.
To be clear, export puts a variable in the current process's environment -- but environment variables are propagated down to child processes, not up to parent processes.
Now, if your goal is to define an interactive command that's easy to call, you might want to consider an entirely different approach altogether -- putting a function in your .bashrc:
awsSetup() {
echo -n "Enter AWS_ACCESS_KEY_ID: "
read && [[ $REPLY ]] && export AWS_ACCESS_KEY_ID=$REPLY
}
...after which the user with this in their .bashrc can run awsSetup, which will run in the current shell.

Simple Bash Script Error and Advice - Saving Environment Variables in Linux

I am working on a project that is hosted in Heroku. The app is hard coded to use Amazon S3 and looks for the keys in environment variables. This is what I wrote after looking at some examples and I am not sure why its not working.
echo $1
if [ $1 != "unset" ]; then
echo "set"
export AMAZON_ACCESS_KEY_ID=XXXXXXXXXXXX
export AMAZON_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
export S3_BUCKET_NAME=XXXXXXXXX
else
echo "unset"
export AMAZON_ACCESS_KEY_ID=''
export AMAZON_SECRET_ACCESS_KEY=''
export S3_BUCKET_NAME=''
fi
When running the script it goes to the set section. But when inspecting through echo $AMAZON_ACCESS_KEY_ID # => ''.
I am not sure what is causing the issue. I will be interested in...
A fix for this...
A way to extract and add heroku config variables in the the env in an easier way.
You need to source the script, not run it as a child. If you run the script directly, its environment disappears when it ends. Sourcing the script causes it to be executed in the current environment. help source for more information.
Example:
$ VAR=old_value
$ cat script.sh
#!/bin/bash
export VAR=new_value
$ ./script.sh
$ echo $VAR
old_value
$ source script.sh
$ echo $VAR
new_value
Scripts executed with source don't need to be executable nor do they need the "shebang" line (#!/bin/bash) because they are not run as separate processes. In fact, it is probably a good idea to not make them executable in order to avoid them being run as commands, since that won't work as expected.

How to write a bash script to set global environment variable?

Recently I wrote a script which sets an environment variable, take a look:
#!/bin/bash
echo "Pass a path:"
read path
echo $path
defaultPath=/home/$(whoami)/Desktop
if [ -n "$path" ]; then
export my_var=$path
else
echo "Path is empty! Exporting default path ..."
export my_var=$defaultPath
fi
echo "Exported path: $my_var"
It works just great but the problem is that my_var is available just locally, I mean in console window where I ran the script.
How to write a script which allow me to export global environment variable which can be seen everywhere?
Just run your shell script preceded by "." (dot space).
This causes the script to run the instructions in the original shell. Thus the variables still exist after the script finish
Ex:
cat setmyvar.sh
export myvar=exists
. ./setmyvar.sh
echo $myvar
exists
Each and every shell has its own environment. There's no Universal environment that will magically appear in all console windows. An environment variable created in one shell cannot be accessed in another shell.
It's even more restrictive. If one shell spawns a subshell, that subshell has access to the parent's environment variables, but if that subshell creates an environment variable, it's not accessible in the parent shell.
If all of your shells need access to the same set of variables, you can create a startup file that will set them for you. This is done in BASH via the $HOME/.bash_profile file (or through $HOME/.profile if $HOME/.bash_profile doesn't exist) or through $HOME/.bashrc. Other shells have their own set of startup files. One is used for logins, and one is used for shells spawned without logins (and, as with bash, a third for non-interactive shells). See the manpage to learn exactly what startup scripts are used and what order they're executed).
You can try using shared memory, but I believe that only works while processes are running, so even if you figured out a way to set a piece of shared memory, it would go away as soon as that command is finished. (I've rarely used shared memory except for named pipes). Otherwise, there's really no way to set an environment variable in one shell and have another shell automatically pick it up. You can try using named pipes or writing that environment variable to a file for other shells to pick it up.
Imagine the problems that could happen if someone could change the environment of one shell without my knowledge.
Actually I found an way to achieve this (which in my case was to use a bash script to set a number of security credentials)
I just call bash from inside the script and the spawned shell now has the export values
export API_USERNAME=abc
export API_PASSWORD=bbbb
bash
now calling the file using ~/.app-x-setup.sh will give me an interactive shell with those environment values setup
The following were extracted from 2nd paragraph from David W.'s answer: "If one shell spawns a subshell, that subshell has access to the parent's environment variables, but if that subshell creates an environment variable, it's not accessible in the parent shell."
In case a user need to let parent shell access your new environment variables, just issue the following command in parent shell:
source <your_subshell_script>
or using shortcut
. <your_subshell_script>
You got to add the variable in your .profile located in /home/$USER/.profile
Yo can do that with this command:
echo 'TEST="hi"' >> $HOME/.profile
Or by edit the file with emacs, for example.
If you want to set this variable for all users, you got to edit /etc/profile (root)
There is no global environment, really, in UNIX.
Each process has an environment, originally inherited from the parent, but it is local to the process after the initial creation.
You can only modify your own, unless you go digging around in the process using a debugger.
write it to a temporary file, lets say ~/.myglobalvar and read it from anywhere
echo "$myglobal" > ~/.myglobalvar
Environment variables are always "local" to process execution the export command allow to set environment variables for sub processes. You can look at .bashrc to set environment variables at the start of a bash shell. What you are trying to do seems not possible as a process cannot modify (or access ?) to environment variables of another process.
You can update the ~/.bashrc or ~/.bash_profile file which is used to initialize the environment.
Take a look at the loading behavior of your shell (explained in the manpage, usually referring to .XXXshrc or .profile). Some configuration files are loaded at login time of an interactive shell, some are loaded each time you run a shell. Placing your variable in the latter might result in the behavior you want, e.g. always having the variable set using that distinct shell (for example bash).
If you need to dynamically set and reference environment variables in shell scripts, there is a work around. Judge for yourself whether is worth doing, but here it is.
The strategy involves having a 'set' script which dynamically writes a 'load' script, which has code to set and export an environment variable. The 'load' script is then executed periodically by other scripts which need to reference the variable. BTW, the same strategy could be done by writing and reading a file instead of a variable.
Here's a quick example...
Set_Load_PROCESSING_SIGNAL.sh
#!/bin/bash
PROCESSING_SIGNAL_SCRIPT=./Load_PROCESSING_SIGNAL.sh
echo "#!/bin/bash" > $PROCESSING_SIGNAL_SCRIPT
echo "export PROCESSING_SIGNAL=$1" >> $PROCESSING_SIGNAL_SCRIPT
chmod ug+rwx $PROCESSING_SIGNAL_SCRIPT
Load_PROCESSING_SIGNAL.sh (this gets dynamically created when the above is run)
#!/bin/bash
export PROCESSING_SIGNAL=1
You can test this with
Test_PROCESSING_SIGNAL.sh
#!/bin/bash
PROCESSING_SIGNAL_SCRIPT=./Load_PROCESSING_SIGNAL.sh
N=1
LIM=100
while [ $N -le $LIM ]
do
# DO WHATEVER LOOP PROCESSING IS NEEDED
echo "N = $N"
sleep 5
N=$(( $N + 1 ))
# CHECK PROCESSING_SIGNAL
source $PROCESSING_SIGNAL_SCRIPT
if [[ $PROCESSING_SIGNAL -eq 0 ]]; then
# Write log info indicating that the signal to stop processing was detected
# Write out all relevent info
# Send an alert email of this too
# Then exit
echo "Detected PROCESSING_SIGNAL for all stop. Exiting..."
exit 1
fi
done
~/.bin/SOURCED/lazy script to save and load data as flat files for system.
[ ! -d ~/.megadata ] && mkdir ~/.megadata
function save_data {
[ -z "$1" -o -z "$2" ] && echo 'save_data [:id:] [:data:]' && return
local overwrite=${3-false}
[ "$overwrite" = 'true' ] && echo "$2" > ~/.megadata/$1 && return
[ ! -f ~/.megadata/$1 ] && echo "$2" > ~/.megadata/$1 || echo ID TAKEN set third param to true to overwrite
}
save_data computer engine
cat ~/.megadata/computer
save_data computer engine
save_data computer megaengine true
function get_data {
[ -z "$1" -o -f $1 ] && echo 'get_data [:id:]' && return
[ -f ~/.megadata/$1 ] && cat ~/.megadata/$1 || echo ID NOT FOUND
:
}
get_data computer
get_data computer
Maybe a little off topic, but when you really need it to set it temporarily to execute some script and ended up here looking for answers:
If you need to run a script with certain environment variables that you don't need to keep after execution you could do something like this:
#!/usr/bin/env sh
export XDEBUG_SESSION=$(hostname);echo "running with xdebug: $XDEBUG_SESSION";$#
In my example I just use XDEBUG_SESSION with a hostname, but you can use multiple variables. Keep them separated with a semi-colon. Execution as follows (assuming you called the script debug.sh and placed it in the same directory as your php script):
$ debug.sh php yourscript.php

Want to export environment variable from startup script to other shells

I'm working on an embedded system using Busybox as the shell. My startup script rcS exports a number of variables:
UBOOT_ENV="gatewayip netmask netdev ipaddr ethaddr eth1addr hostname nfsaddr"
for i in $UBOOT_ENV; do
if [ -n "$i" ] ; then
export `fw_printenv $i`
fi
done
which are then available to scripts called from this script as I'd expect. What I want however is for these environment variables to be set in the environment for which some web server scripts are called. This is currently not the case. How do I make an environment variable available to any shell script called?
TY,
Fred
ps : my busybox is BusyBox v1.11.2 (2012-02-26 12:08:09 PST) built-in shell (msh)
Environment variables are only inherited by child processes of your script (and their child processes); you can't push them up to a parent process.
What you can do is write the variables to a file (as a shell script) which you can then include from wherever you like. Put source filename in /etc/.profile and it will probably do what you want.

Resources