LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2022, 07:28 AM   #1
paulspedding
LQ Newbie
 
Registered: Dec 2021
Posts: 21

Rep: Reputation: Disabled
Global bash varible not current shell or sub shell.


So I have tried export and declare and both don't seem to be setting a truly globalvar. I want to be able to access this varible from outside the script its set in.

How can I set a global varible so when the script has ended I can still access it from outside so other scripts can use it?

For instance I'm getting an Auth Token but I written smaller scripts with individual functions to use that Auth Token.

Thanks.
 
Old 05-30-2022, 07:29 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,033

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
would be nice to see exactly how did you set and what and also how did you try to use that later.
 
Old 05-30-2022, 07:43 AM   #3
paulspedding
LQ Newbie
 
Registered: Dec 2021
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
would be nice to see exactly how did you set and what and also how did you try to use that later.

#!/bin/bash
AUTH_TOKEN=$(this gets my auth token)
declare -g AUTH_TOKEN="$AUTH_TOKEN"
And then when I goto view it I use
echo "$AUTH_TOKEN"
I have tried the -x an -g and AFAIK the '-x' is the same as using:
export AUTH_TOKEN="$AUTH_TOKEN"
Isn't it? I also just tried using export but it didnt work either.

I was using to view the varible using both the following:

printenv
OR
declare -p
But I could never see them set. I did try to use `echo "$AUTH_TOKEN"` a few times but that yeilded empty results.
 
Old 05-30-2022, 07:49 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,033

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
still unclear, but some comments:
Quote:
Originally Posted by paulspedding View Post

#!/bin/bash
AUTH_TOKEN=$(this gets my auth token)
declare -g AUTH_TOKEN="$AUTH_TOKEN"
Is this a script you executed? So it will set AUTH_TOKEN, but it will not be available in the parent shell anyway. You need to use source if you wish to modify the environment of the current shell.
Quote:
Originally Posted by paulspedding View Post
And then when I goto view it I use
echo "$AUTH_TOKEN"
I have tried the -x an -g and AFAIK the '-x' is the same as using:
export AUTH_TOKEN="$AUTH_TOKEN"
Isn't it? I also just tried using export but it didnt work either.
I have no idea what do you mean by that. How did you try -x and -g ?
Quote:
Originally Posted by paulspedding View Post
I was using to view the varible using both the following:

printenv
OR
declare -p
But I could never see them set. I did try to use `echo "$AUTH_TOKEN"` a few times but that yeilded empty results.
Again, I think you set the variable in a subshell, and checks the result in the current shell. But it is only a guess, I still don't understand what did you do.
 
Old 05-30-2022, 11:29 AM   #5
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Quote:
I want to be able to access this varible from outside the script its set in
one.sh
Code:
#!/usr/bin/bash

fred="Flintstone"
barney="Rubble"

wilma() {
    echo "Hello Wilma"
}
two.sh
Code:
#!/usr/bin/bash

source one.sh

echo "$fred"
echo "$barney"

wilma
Code:
bash ./two.sh
Flintstone
Rubble
Hello Wilma
 
Old 05-30-2022, 11:35 AM   #6
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
three.sh
Code:
#!/usr/bin/bash

a=$(echo "Hello")
b=$(echo "World")

c() {
    echo "I like Linux"
}
Code:
source three.sh
echo "$a"
Hello
echo "$b"
World
c
I like Linux
 
Old 05-30-2022, 12:12 PM   #7
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
four.sh
Code:
#!/usr/bin/bash

echo "expVar = $expVar"
echo "myVar = $myVar"

export expVar="Exported variable"
myVar="My variable"
Code:
bash ./four.sh
expVar = Exported variable
myVar =

echo "$expVar"
Exported variable

source four.sh
expVar = Exported variable
myVar = My variable
 
Old 05-30-2022, 02:15 PM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ That's one way to do it.
Another way to do it is to simply save the token to a (temporary) file.

A login shell can define global variables in the sense that all programs started under it will inherit its environment.

Defining a shell variable that somehow becomes valid outside of its scope is not possible.
 
Old 06-01-2022, 12:32 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,830

Rep: Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216
In Unix-like systems there is no "globalvar".
Environment (including environment variables) is inherited by new forked processes (child processes) only. Not by processes that are outside.
You have to save/import it to/from a file. (Or use a DB service like dconf or polkit that also use a file for storage.)
In bash you can save environment-setting code to .bashrc then all new interactive bash processes (and their child processes) will get it.
A "globalvar"-like environment can be set for pid 1 (init or systemd) then all processes will inherit it. But inherit means copy; a change in the parent process will not arrive at the already forked child processes.
 
  


Reply

Tags
bash, scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script with Menu and sub menu and in sub menu execute another shell script SHWE Linux - Newbie 9 11-03-2018 06:19 PM
when to know we are going to work in sub shell or would be entering sub shell sysmicuser Linux - Newbie 8 05-07-2013 11:08 AM
Shell Script to compare folders,Sub-Folders and Sub-Sub-Folders unix_72427 Programming 8 08-08-2012 02:51 PM
perl script get enviromnet varible LINES= for current session zerobane Programming 6 04-02-2009 05:23 PM
Setting global varible Corrado Linux - Software 3 03-05-2007 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration