Is it possible to change the value of environment variable from a bash script so that it persists?
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
0
down vote
favorite
I would like to use an environment variable I have declared in /etc/environment
as a counter which should be accessible to several unrelated scripts that are run at different times.
Is that possible? So far, I had no luck trying exporting its value.
I guess, I could always use a temporary file to store current value, read/write to it but I'm looking for a more elegant way if there is any?
command-line bash scripts environment-variables
add a comment |Â
up vote
0
down vote
favorite
I would like to use an environment variable I have declared in /etc/environment
as a counter which should be accessible to several unrelated scripts that are run at different times.
Is that possible? So far, I had no luck trying exporting its value.
I guess, I could always use a temporary file to store current value, read/write to it but I'm looking for a more elegant way if there is any?
command-line bash scripts environment-variables
2
What happens if two scripts try to change the counter simultaneously? IâÂÂd rather use a file which contains the counter value and lock it as explained here and here.
â dessert
May 15 at 11:50
mktemp
is elegance in its purest form. ;)
â dessert
May 15 at 12:00
1
The issue probably isn't persistence - it's that the other scripts will need to re-source their environment for any modified value to take effect
â steeldriver
May 15 at 12:00
Good point, but I'd take care that doesn't happen. I agree, using a file instead is a way to achieve the same although, what happens if two scripts try to change the value in the file simultaneously ;) I'm mostly asking to improve my understanding of how environment is working. And it would be wonderful if it is somehow possible.
â S.R.
May 15 at 12:00
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to use an environment variable I have declared in /etc/environment
as a counter which should be accessible to several unrelated scripts that are run at different times.
Is that possible? So far, I had no luck trying exporting its value.
I guess, I could always use a temporary file to store current value, read/write to it but I'm looking for a more elegant way if there is any?
command-line bash scripts environment-variables
I would like to use an environment variable I have declared in /etc/environment
as a counter which should be accessible to several unrelated scripts that are run at different times.
Is that possible? So far, I had no luck trying exporting its value.
I guess, I could always use a temporary file to store current value, read/write to it but I'm looking for a more elegant way if there is any?
command-line bash scripts environment-variables
edited May 15 at 11:54
asked May 15 at 11:39
S.R.
10610
10610
2
What happens if two scripts try to change the counter simultaneously? IâÂÂd rather use a file which contains the counter value and lock it as explained here and here.
â dessert
May 15 at 11:50
mktemp
is elegance in its purest form. ;)
â dessert
May 15 at 12:00
1
The issue probably isn't persistence - it's that the other scripts will need to re-source their environment for any modified value to take effect
â steeldriver
May 15 at 12:00
Good point, but I'd take care that doesn't happen. I agree, using a file instead is a way to achieve the same although, what happens if two scripts try to change the value in the file simultaneously ;) I'm mostly asking to improve my understanding of how environment is working. And it would be wonderful if it is somehow possible.
â S.R.
May 15 at 12:00
add a comment |Â
2
What happens if two scripts try to change the counter simultaneously? IâÂÂd rather use a file which contains the counter value and lock it as explained here and here.
â dessert
May 15 at 11:50
mktemp
is elegance in its purest form. ;)
â dessert
May 15 at 12:00
1
The issue probably isn't persistence - it's that the other scripts will need to re-source their environment for any modified value to take effect
â steeldriver
May 15 at 12:00
Good point, but I'd take care that doesn't happen. I agree, using a file instead is a way to achieve the same although, what happens if two scripts try to change the value in the file simultaneously ;) I'm mostly asking to improve my understanding of how environment is working. And it would be wonderful if it is somehow possible.
â S.R.
May 15 at 12:00
2
2
What happens if two scripts try to change the counter simultaneously? IâÂÂd rather use a file which contains the counter value and lock it as explained here and here.
â dessert
May 15 at 11:50
What happens if two scripts try to change the counter simultaneously? IâÂÂd rather use a file which contains the counter value and lock it as explained here and here.
â dessert
May 15 at 11:50
mktemp
is elegance in its purest form. ;)â dessert
May 15 at 12:00
mktemp
is elegance in its purest form. ;)â dessert
May 15 at 12:00
1
1
The issue probably isn't persistence - it's that the other scripts will need to re-source their environment for any modified value to take effect
â steeldriver
May 15 at 12:00
The issue probably isn't persistence - it's that the other scripts will need to re-source their environment for any modified value to take effect
â steeldriver
May 15 at 12:00
Good point, but I'd take care that doesn't happen. I agree, using a file instead is a way to achieve the same although, what happens if two scripts try to change the value in the file simultaneously ;) I'm mostly asking to improve my understanding of how environment is working. And it would be wonderful if it is somehow possible.
â S.R.
May 15 at 12:00
Good point, but I'd take care that doesn't happen. I agree, using a file instead is a way to achieve the same although, what happens if two scripts try to change the value in the file simultaneously ;) I'm mostly asking to improve my understanding of how environment is working. And it would be wonderful if it is somehow possible.
â S.R.
May 15 at 12:00
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The short answer is "No, you can't permanently change an environment variable from within a Bash script".
The longer answer is that when a Bash script is executed it receives a copy of the environment (not references to the environment variables themselves). So, whilst you can change the values of the copies within your script, those changes will be lost when the script exits and are also not visible to other scripts you may be running concurrently.
You can however, change an environment variable with a Bash script by "source"-ing it from the command line:
source your_script_name
Thanks Peter, I just took a look at "source-ing" and it doesn't cut it for this purpose. I need to pass a variable from a Nemo script to a script that is executed by an applet so, the only viable way seems to be the use of a temporary file as dessert pinpointed.
â S.R.
May 15 at 12:45
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The short answer is "No, you can't permanently change an environment variable from within a Bash script".
The longer answer is that when a Bash script is executed it receives a copy of the environment (not references to the environment variables themselves). So, whilst you can change the values of the copies within your script, those changes will be lost when the script exits and are also not visible to other scripts you may be running concurrently.
You can however, change an environment variable with a Bash script by "source"-ing it from the command line:
source your_script_name
Thanks Peter, I just took a look at "source-ing" and it doesn't cut it for this purpose. I need to pass a variable from a Nemo script to a script that is executed by an applet so, the only viable way seems to be the use of a temporary file as dessert pinpointed.
â S.R.
May 15 at 12:45
add a comment |Â
up vote
1
down vote
accepted
The short answer is "No, you can't permanently change an environment variable from within a Bash script".
The longer answer is that when a Bash script is executed it receives a copy of the environment (not references to the environment variables themselves). So, whilst you can change the values of the copies within your script, those changes will be lost when the script exits and are also not visible to other scripts you may be running concurrently.
You can however, change an environment variable with a Bash script by "source"-ing it from the command line:
source your_script_name
Thanks Peter, I just took a look at "source-ing" and it doesn't cut it for this purpose. I need to pass a variable from a Nemo script to a script that is executed by an applet so, the only viable way seems to be the use of a temporary file as dessert pinpointed.
â S.R.
May 15 at 12:45
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The short answer is "No, you can't permanently change an environment variable from within a Bash script".
The longer answer is that when a Bash script is executed it receives a copy of the environment (not references to the environment variables themselves). So, whilst you can change the values of the copies within your script, those changes will be lost when the script exits and are also not visible to other scripts you may be running concurrently.
You can however, change an environment variable with a Bash script by "source"-ing it from the command line:
source your_script_name
The short answer is "No, you can't permanently change an environment variable from within a Bash script".
The longer answer is that when a Bash script is executed it receives a copy of the environment (not references to the environment variables themselves). So, whilst you can change the values of the copies within your script, those changes will be lost when the script exits and are also not visible to other scripts you may be running concurrently.
You can however, change an environment variable with a Bash script by "source"-ing it from the command line:
source your_script_name
edited May 15 at 15:43
muru
129k19271460
129k19271460
answered May 15 at 12:25
![](https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=32)
![](https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=32)
Peter Lord
261
261
Thanks Peter, I just took a look at "source-ing" and it doesn't cut it for this purpose. I need to pass a variable from a Nemo script to a script that is executed by an applet so, the only viable way seems to be the use of a temporary file as dessert pinpointed.
â S.R.
May 15 at 12:45
add a comment |Â
Thanks Peter, I just took a look at "source-ing" and it doesn't cut it for this purpose. I need to pass a variable from a Nemo script to a script that is executed by an applet so, the only viable way seems to be the use of a temporary file as dessert pinpointed.
â S.R.
May 15 at 12:45
Thanks Peter, I just took a look at "source-ing" and it doesn't cut it for this purpose. I need to pass a variable from a Nemo script to a script that is executed by an applet so, the only viable way seems to be the use of a temporary file as dessert pinpointed.
â S.R.
May 15 at 12:45
Thanks Peter, I just took a look at "source-ing" and it doesn't cut it for this purpose. I need to pass a variable from a Nemo script to a script that is executed by an applet so, the only viable way seems to be the use of a temporary file as dessert pinpointed.
â S.R.
May 15 at 12:45
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1036496%2fis-it-possible-to-change-the-value-of-environment-variable-from-a-bash-script-so%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
What happens if two scripts try to change the counter simultaneously? IâÂÂd rather use a file which contains the counter value and lock it as explained here and here.
â dessert
May 15 at 11:50
mktemp
is elegance in its purest form. ;)â dessert
May 15 at 12:00
1
The issue probably isn't persistence - it's that the other scripts will need to re-source their environment for any modified value to take effect
â steeldriver
May 15 at 12:00
Good point, but I'd take care that doesn't happen. I agree, using a file instead is a way to achieve the same although, what happens if two scripts try to change the value in the file simultaneously ;) I'm mostly asking to improve my understanding of how environment is working. And it would be wonderful if it is somehow possible.
â S.R.
May 15 at 12:00