How to export variables from a file?

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











up vote
43
down vote

favorite
16












I have a tmp.txt file containing variables to be exported, for example:



a=123
b="hello world"
c="one more variable"


How can I export all these variables using the export command, so that they can later be used by child processes?










share|improve this question























  • bash --init-file somefile.blah -c "command"
    – Dillian Murphey
    Apr 14 '16 at 22:33














up vote
43
down vote

favorite
16












I have a tmp.txt file containing variables to be exported, for example:



a=123
b="hello world"
c="one more variable"


How can I export all these variables using the export command, so that they can later be used by child processes?










share|improve this question























  • bash --init-file somefile.blah -c "command"
    – Dillian Murphey
    Apr 14 '16 at 22:33












up vote
43
down vote

favorite
16









up vote
43
down vote

favorite
16






16





I have a tmp.txt file containing variables to be exported, for example:



a=123
b="hello world"
c="one more variable"


How can I export all these variables using the export command, so that they can later be used by child processes?










share|improve this question















I have a tmp.txt file containing variables to be exported, for example:



a=123
b="hello world"
c="one more variable"


How can I export all these variables using the export command, so that they can later be used by child processes?







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 20 at 8:48









Marco Bonelli

23512




23512










asked Jun 11 '13 at 20:17









Neerav

485267




485267











  • bash --init-file somefile.blah -c "command"
    – Dillian Murphey
    Apr 14 '16 at 22:33
















  • bash --init-file somefile.blah -c "command"
    – Dillian Murphey
    Apr 14 '16 at 22:33















bash --init-file somefile.blah -c "command"
– Dillian Murphey
Apr 14 '16 at 22:33




bash --init-file somefile.blah -c "command"
– Dillian Murphey
Apr 14 '16 at 22:33










2 Answers
2






active

oldest

votes

















up vote
48
down vote



accepted










source tmp.txt
export a b c
./child ...



Judging by your other question, you don't want to hardcode the variable names:



source tmp.txt
export $(cut -d= -f1 tmp.txt)


test it:



$ source tmp.txt
$ echo "$a $b $c"
123 hello world one more variable
$ perl -E 'say "@ENVqw(a b c)"'

$ export $(cut -d= -f1 tmp.txt)
$ perl -E 'say "@ENVqw(a b c)"'
123 hello world one more variable





share|improve this answer


















  • 2




    This won't work if the environment file contains comments, for example. (eg. files that can be reused by systemd's EnvironmentFile)
    – Chris Lamb
    Nov 12 '17 at 22:36










  • @ChrisLamb you can use grep to skip comments: export $(grep --regexp ^[A-Z] tmp.txt | cut -d= -f1)
    – gvee
    Jul 2 at 16:05


















up vote
118
down vote













set -a
. ./tmp.txt
set +a


set -a causes variables¹ defined from now on to be automatically exported. It's available in any Bourne-like shell. . is the standard and Bourne name for the source command so I prefer it for portability (source comes from csh and is now available in most modern Bourne-like shells including bash though (sometimes with a slightly different behaviour)).



In POSIX shells, you can also use set -o allexport as a more descriptive alternative way to write it (set +o allexport to unset).




¹ In bash, be aware that, while allexport is on, all functions that have been declared (which are also variables) will also be exported to the environment (as BASH_FUNC_myfunction%% environment variables that are then imported by all bash shells run in that environment, even when running as sh).






share|improve this answer


















  • 9




    You could also do set +a afterwards to turn it back off
    – Daniel Worthington-Bodart
    Sep 11 '15 at 14:18






  • 5




    I hope it goes without saying that this tools-based approach is superior to the "chosen" programmatic approach
    – Douglas Held
    Oct 5 '15 at 13:16










  • @DanielWorthington-Bodart, good point. Included.
    – Stéphane Chazelas
    Jun 23 '16 at 16:05






  • 2




    This answer should definitely be marked as correct. Thanks for providing this!
    – Anson MacKeracher
    Feb 8 '17 at 15:10










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f79064%2fhow-to-export-variables-from-a-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
48
down vote



accepted










source tmp.txt
export a b c
./child ...



Judging by your other question, you don't want to hardcode the variable names:



source tmp.txt
export $(cut -d= -f1 tmp.txt)


test it:



$ source tmp.txt
$ echo "$a $b $c"
123 hello world one more variable
$ perl -E 'say "@ENVqw(a b c)"'

$ export $(cut -d= -f1 tmp.txt)
$ perl -E 'say "@ENVqw(a b c)"'
123 hello world one more variable





share|improve this answer


















  • 2




    This won't work if the environment file contains comments, for example. (eg. files that can be reused by systemd's EnvironmentFile)
    – Chris Lamb
    Nov 12 '17 at 22:36










  • @ChrisLamb you can use grep to skip comments: export $(grep --regexp ^[A-Z] tmp.txt | cut -d= -f1)
    – gvee
    Jul 2 at 16:05















up vote
48
down vote



accepted










source tmp.txt
export a b c
./child ...



Judging by your other question, you don't want to hardcode the variable names:



source tmp.txt
export $(cut -d= -f1 tmp.txt)


test it:



$ source tmp.txt
$ echo "$a $b $c"
123 hello world one more variable
$ perl -E 'say "@ENVqw(a b c)"'

$ export $(cut -d= -f1 tmp.txt)
$ perl -E 'say "@ENVqw(a b c)"'
123 hello world one more variable





share|improve this answer


















  • 2




    This won't work if the environment file contains comments, for example. (eg. files that can be reused by systemd's EnvironmentFile)
    – Chris Lamb
    Nov 12 '17 at 22:36










  • @ChrisLamb you can use grep to skip comments: export $(grep --regexp ^[A-Z] tmp.txt | cut -d= -f1)
    – gvee
    Jul 2 at 16:05













up vote
48
down vote



accepted







up vote
48
down vote



accepted






source tmp.txt
export a b c
./child ...



Judging by your other question, you don't want to hardcode the variable names:



source tmp.txt
export $(cut -d= -f1 tmp.txt)


test it:



$ source tmp.txt
$ echo "$a $b $c"
123 hello world one more variable
$ perl -E 'say "@ENVqw(a b c)"'

$ export $(cut -d= -f1 tmp.txt)
$ perl -E 'say "@ENVqw(a b c)"'
123 hello world one more variable





share|improve this answer














source tmp.txt
export a b c
./child ...



Judging by your other question, you don't want to hardcode the variable names:



source tmp.txt
export $(cut -d= -f1 tmp.txt)


test it:



$ source tmp.txt
$ echo "$a $b $c"
123 hello world one more variable
$ perl -E 'say "@ENVqw(a b c)"'

$ export $(cut -d= -f1 tmp.txt)
$ perl -E 'say "@ENVqw(a b c)"'
123 hello world one more variable






share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 11 '13 at 20:55

























answered Jun 11 '13 at 20:21









glenn jackman

50.1k569106




50.1k569106







  • 2




    This won't work if the environment file contains comments, for example. (eg. files that can be reused by systemd's EnvironmentFile)
    – Chris Lamb
    Nov 12 '17 at 22:36










  • @ChrisLamb you can use grep to skip comments: export $(grep --regexp ^[A-Z] tmp.txt | cut -d= -f1)
    – gvee
    Jul 2 at 16:05













  • 2




    This won't work if the environment file contains comments, for example. (eg. files that can be reused by systemd's EnvironmentFile)
    – Chris Lamb
    Nov 12 '17 at 22:36










  • @ChrisLamb you can use grep to skip comments: export $(grep --regexp ^[A-Z] tmp.txt | cut -d= -f1)
    – gvee
    Jul 2 at 16:05








2




2




This won't work if the environment file contains comments, for example. (eg. files that can be reused by systemd's EnvironmentFile)
– Chris Lamb
Nov 12 '17 at 22:36




This won't work if the environment file contains comments, for example. (eg. files that can be reused by systemd's EnvironmentFile)
– Chris Lamb
Nov 12 '17 at 22:36












@ChrisLamb you can use grep to skip comments: export $(grep --regexp ^[A-Z] tmp.txt | cut -d= -f1)
– gvee
Jul 2 at 16:05





@ChrisLamb you can use grep to skip comments: export $(grep --regexp ^[A-Z] tmp.txt | cut -d= -f1)
– gvee
Jul 2 at 16:05













up vote
118
down vote













set -a
. ./tmp.txt
set +a


set -a causes variables¹ defined from now on to be automatically exported. It's available in any Bourne-like shell. . is the standard and Bourne name for the source command so I prefer it for portability (source comes from csh and is now available in most modern Bourne-like shells including bash though (sometimes with a slightly different behaviour)).



In POSIX shells, you can also use set -o allexport as a more descriptive alternative way to write it (set +o allexport to unset).




¹ In bash, be aware that, while allexport is on, all functions that have been declared (which are also variables) will also be exported to the environment (as BASH_FUNC_myfunction%% environment variables that are then imported by all bash shells run in that environment, even when running as sh).






share|improve this answer


















  • 9




    You could also do set +a afterwards to turn it back off
    – Daniel Worthington-Bodart
    Sep 11 '15 at 14:18






  • 5




    I hope it goes without saying that this tools-based approach is superior to the "chosen" programmatic approach
    – Douglas Held
    Oct 5 '15 at 13:16










  • @DanielWorthington-Bodart, good point. Included.
    – Stéphane Chazelas
    Jun 23 '16 at 16:05






  • 2




    This answer should definitely be marked as correct. Thanks for providing this!
    – Anson MacKeracher
    Feb 8 '17 at 15:10














up vote
118
down vote













set -a
. ./tmp.txt
set +a


set -a causes variables¹ defined from now on to be automatically exported. It's available in any Bourne-like shell. . is the standard and Bourne name for the source command so I prefer it for portability (source comes from csh and is now available in most modern Bourne-like shells including bash though (sometimes with a slightly different behaviour)).



In POSIX shells, you can also use set -o allexport as a more descriptive alternative way to write it (set +o allexport to unset).




¹ In bash, be aware that, while allexport is on, all functions that have been declared (which are also variables) will also be exported to the environment (as BASH_FUNC_myfunction%% environment variables that are then imported by all bash shells run in that environment, even when running as sh).






share|improve this answer


















  • 9




    You could also do set +a afterwards to turn it back off
    – Daniel Worthington-Bodart
    Sep 11 '15 at 14:18






  • 5




    I hope it goes without saying that this tools-based approach is superior to the "chosen" programmatic approach
    – Douglas Held
    Oct 5 '15 at 13:16










  • @DanielWorthington-Bodart, good point. Included.
    – Stéphane Chazelas
    Jun 23 '16 at 16:05






  • 2




    This answer should definitely be marked as correct. Thanks for providing this!
    – Anson MacKeracher
    Feb 8 '17 at 15:10












up vote
118
down vote










up vote
118
down vote









set -a
. ./tmp.txt
set +a


set -a causes variables¹ defined from now on to be automatically exported. It's available in any Bourne-like shell. . is the standard and Bourne name for the source command so I prefer it for portability (source comes from csh and is now available in most modern Bourne-like shells including bash though (sometimes with a slightly different behaviour)).



In POSIX shells, you can also use set -o allexport as a more descriptive alternative way to write it (set +o allexport to unset).




¹ In bash, be aware that, while allexport is on, all functions that have been declared (which are also variables) will also be exported to the environment (as BASH_FUNC_myfunction%% environment variables that are then imported by all bash shells run in that environment, even when running as sh).






share|improve this answer














set -a
. ./tmp.txt
set +a


set -a causes variables¹ defined from now on to be automatically exported. It's available in any Bourne-like shell. . is the standard and Bourne name for the source command so I prefer it for portability (source comes from csh and is now available in most modern Bourne-like shells including bash though (sometimes with a slightly different behaviour)).



In POSIX shells, you can also use set -o allexport as a more descriptive alternative way to write it (set +o allexport to unset).




¹ In bash, be aware that, while allexport is on, all functions that have been declared (which are also variables) will also be exported to the environment (as BASH_FUNC_myfunction%% environment variables that are then imported by all bash shells run in that environment, even when running as sh).







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 9 at 3:15









Jamieson Becker

1626




1626










answered Jun 11 '13 at 21:13









Stéphane Chazelas

298k54562910




298k54562910







  • 9




    You could also do set +a afterwards to turn it back off
    – Daniel Worthington-Bodart
    Sep 11 '15 at 14:18






  • 5




    I hope it goes without saying that this tools-based approach is superior to the "chosen" programmatic approach
    – Douglas Held
    Oct 5 '15 at 13:16










  • @DanielWorthington-Bodart, good point. Included.
    – Stéphane Chazelas
    Jun 23 '16 at 16:05






  • 2




    This answer should definitely be marked as correct. Thanks for providing this!
    – Anson MacKeracher
    Feb 8 '17 at 15:10












  • 9




    You could also do set +a afterwards to turn it back off
    – Daniel Worthington-Bodart
    Sep 11 '15 at 14:18






  • 5




    I hope it goes without saying that this tools-based approach is superior to the "chosen" programmatic approach
    – Douglas Held
    Oct 5 '15 at 13:16










  • @DanielWorthington-Bodart, good point. Included.
    – Stéphane Chazelas
    Jun 23 '16 at 16:05






  • 2




    This answer should definitely be marked as correct. Thanks for providing this!
    – Anson MacKeracher
    Feb 8 '17 at 15:10







9




9




You could also do set +a afterwards to turn it back off
– Daniel Worthington-Bodart
Sep 11 '15 at 14:18




You could also do set +a afterwards to turn it back off
– Daniel Worthington-Bodart
Sep 11 '15 at 14:18




5




5




I hope it goes without saying that this tools-based approach is superior to the "chosen" programmatic approach
– Douglas Held
Oct 5 '15 at 13:16




I hope it goes without saying that this tools-based approach is superior to the "chosen" programmatic approach
– Douglas Held
Oct 5 '15 at 13:16












@DanielWorthington-Bodart, good point. Included.
– Stéphane Chazelas
Jun 23 '16 at 16:05




@DanielWorthington-Bodart, good point. Included.
– Stéphane Chazelas
Jun 23 '16 at 16:05




2




2




This answer should definitely be marked as correct. Thanks for providing this!
– Anson MacKeracher
Feb 8 '17 at 15:10




This answer should definitely be marked as correct. Thanks for providing this!
– Anson MacKeracher
Feb 8 '17 at 15:10

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f79064%2fhow-to-export-variables-from-a-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay