Is it possible to change the value of environment variable from a bash script so that it persists?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








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?







share|improve this question


















  • 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














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?







share|improve this question


















  • 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












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?







share|improve this question














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?









share|improve this question













share|improve this question




share|improve this question








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












  • 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










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





share|improve this answer






















  • 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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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





share|improve this answer






















  • 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














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





share|improve this answer






















  • 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












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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








edited May 15 at 15:43









muru

129k19271460




129k19271460










answered May 15 at 12:25









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
















  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

pylint3 and pip3 broken

Missing snmpget and snmpwalk

How to enroll fingerprints to Ubuntu 17.10 with VFS491