In bash, how do I list environment variables matching “MY_VAR” and then export them?

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











up vote
0
down vote

favorite












I want to export all environment variables beginning with "MY_VAR_".
How do I do this?










share|improve this question









New contributor




virasana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    The variables whose names begin with that, or the variables whose values begin with that? Also, do you mean environment variables (exported shell variables) in particular, or just all shell variables?
    – ilkkachu
    Nov 20 at 17:05










  • Environment variables are already exported. What do want to achieve in the end?
    – Kusalananda
    Nov 21 at 8:03














up vote
0
down vote

favorite












I want to export all environment variables beginning with "MY_VAR_".
How do I do this?










share|improve this question









New contributor




virasana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    The variables whose names begin with that, or the variables whose values begin with that? Also, do you mean environment variables (exported shell variables) in particular, or just all shell variables?
    – ilkkachu
    Nov 20 at 17:05










  • Environment variables are already exported. What do want to achieve in the end?
    – Kusalananda
    Nov 21 at 8:03












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to export all environment variables beginning with "MY_VAR_".
How do I do this?










share|improve this question









New contributor




virasana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I want to export all environment variables beginning with "MY_VAR_".
How do I do this?







bash environment-variables






share|improve this question









New contributor




virasana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




virasana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 20 at 20:13









Rui F Ribeiro

38.2k1475125




38.2k1475125






New contributor




virasana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 20 at 17:00









virasana

1




1




New contributor




virasana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





virasana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






virasana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    The variables whose names begin with that, or the variables whose values begin with that? Also, do you mean environment variables (exported shell variables) in particular, or just all shell variables?
    – ilkkachu
    Nov 20 at 17:05










  • Environment variables are already exported. What do want to achieve in the end?
    – Kusalananda
    Nov 21 at 8:03












  • 1




    The variables whose names begin with that, or the variables whose values begin with that? Also, do you mean environment variables (exported shell variables) in particular, or just all shell variables?
    – ilkkachu
    Nov 20 at 17:05










  • Environment variables are already exported. What do want to achieve in the end?
    – Kusalananda
    Nov 21 at 8:03







1




1




The variables whose names begin with that, or the variables whose values begin with that? Also, do you mean environment variables (exported shell variables) in particular, or just all shell variables?
– ilkkachu
Nov 20 at 17:05




The variables whose names begin with that, or the variables whose values begin with that? Also, do you mean environment variables (exported shell variables) in particular, or just all shell variables?
– ilkkachu
Nov 20 at 17:05












Environment variables are already exported. What do want to achieve in the end?
– Kusalananda
Nov 21 at 8:03




Environment variables are already exported. What do want to achieve in the end?
– Kusalananda
Nov 21 at 8:03










1 Answer
1






active

oldest

votes

















up vote
4
down vote













As export of an already exported variable is no-op:



export "$!MY_VAR_@"


Will export all variables that start with MY_VAR_.




If you only want the list of exported variables that start with MY_VAR_:



env | grep '^MY_VAR_'


Or (calling an external program only keeps exported variables):



bash -c 'printf %s\n "$!MY_VAR_@"'



Or, if you have completion available (it is loaded by default):



$ compgen -A export MY_VAR_
MY_VAR_aa
MY_VAR_bb
MY_VAR_ss





share|improve this answer






















  • how about sh -c 'echo $!MY_VAR_@' for listing exported variables that start with MY_VAR_
    – iruvar
    Nov 21 at 4:48










  • @iruvar That will only work if sh is linked to bash and better that you quote the expansion of the variable: bash -c 'printf %s\n "$!MY_VAR_@"'.
    – Isaac
    Nov 21 at 4:58











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',
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
);



);






virasana is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483011%2fin-bash-how-do-i-list-environment-variables-matching-my-var-and-then-export-t%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote













As export of an already exported variable is no-op:



export "$!MY_VAR_@"


Will export all variables that start with MY_VAR_.




If you only want the list of exported variables that start with MY_VAR_:



env | grep '^MY_VAR_'


Or (calling an external program only keeps exported variables):



bash -c 'printf %s\n "$!MY_VAR_@"'



Or, if you have completion available (it is loaded by default):



$ compgen -A export MY_VAR_
MY_VAR_aa
MY_VAR_bb
MY_VAR_ss





share|improve this answer






















  • how about sh -c 'echo $!MY_VAR_@' for listing exported variables that start with MY_VAR_
    – iruvar
    Nov 21 at 4:48










  • @iruvar That will only work if sh is linked to bash and better that you quote the expansion of the variable: bash -c 'printf %s\n "$!MY_VAR_@"'.
    – Isaac
    Nov 21 at 4:58















up vote
4
down vote













As export of an already exported variable is no-op:



export "$!MY_VAR_@"


Will export all variables that start with MY_VAR_.




If you only want the list of exported variables that start with MY_VAR_:



env | grep '^MY_VAR_'


Or (calling an external program only keeps exported variables):



bash -c 'printf %s\n "$!MY_VAR_@"'



Or, if you have completion available (it is loaded by default):



$ compgen -A export MY_VAR_
MY_VAR_aa
MY_VAR_bb
MY_VAR_ss





share|improve this answer






















  • how about sh -c 'echo $!MY_VAR_@' for listing exported variables that start with MY_VAR_
    – iruvar
    Nov 21 at 4:48










  • @iruvar That will only work if sh is linked to bash and better that you quote the expansion of the variable: bash -c 'printf %s\n "$!MY_VAR_@"'.
    – Isaac
    Nov 21 at 4:58













up vote
4
down vote










up vote
4
down vote









As export of an already exported variable is no-op:



export "$!MY_VAR_@"


Will export all variables that start with MY_VAR_.




If you only want the list of exported variables that start with MY_VAR_:



env | grep '^MY_VAR_'


Or (calling an external program only keeps exported variables):



bash -c 'printf %s\n "$!MY_VAR_@"'



Or, if you have completion available (it is loaded by default):



$ compgen -A export MY_VAR_
MY_VAR_aa
MY_VAR_bb
MY_VAR_ss





share|improve this answer














As export of an already exported variable is no-op:



export "$!MY_VAR_@"


Will export all variables that start with MY_VAR_.




If you only want the list of exported variables that start with MY_VAR_:



env | grep '^MY_VAR_'


Or (calling an external program only keeps exported variables):



bash -c 'printf %s\n "$!MY_VAR_@"'



Or, if you have completion available (it is loaded by default):



$ compgen -A export MY_VAR_
MY_VAR_aa
MY_VAR_bb
MY_VAR_ss






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 5:02

























answered Nov 20 at 17:23









Isaac

9,70411445




9,70411445











  • how about sh -c 'echo $!MY_VAR_@' for listing exported variables that start with MY_VAR_
    – iruvar
    Nov 21 at 4:48










  • @iruvar That will only work if sh is linked to bash and better that you quote the expansion of the variable: bash -c 'printf %s\n "$!MY_VAR_@"'.
    – Isaac
    Nov 21 at 4:58

















  • how about sh -c 'echo $!MY_VAR_@' for listing exported variables that start with MY_VAR_
    – iruvar
    Nov 21 at 4:48










  • @iruvar That will only work if sh is linked to bash and better that you quote the expansion of the variable: bash -c 'printf %s\n "$!MY_VAR_@"'.
    – Isaac
    Nov 21 at 4:58
















how about sh -c 'echo $!MY_VAR_@' for listing exported variables that start with MY_VAR_
– iruvar
Nov 21 at 4:48




how about sh -c 'echo $!MY_VAR_@' for listing exported variables that start with MY_VAR_
– iruvar
Nov 21 at 4:48












@iruvar That will only work if sh is linked to bash and better that you quote the expansion of the variable: bash -c 'printf %s\n "$!MY_VAR_@"'.
– Isaac
Nov 21 at 4:58





@iruvar That will only work if sh is linked to bash and better that you quote the expansion of the variable: bash -c 'printf %s\n "$!MY_VAR_@"'.
– Isaac
Nov 21 at 4:58











virasana is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















virasana is a new contributor. Be nice, and check out our Code of Conduct.












virasana is a new contributor. Be nice, and check out our Code of Conduct.











virasana is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483011%2fin-bash-how-do-i-list-environment-variables-matching-my-var-and-then-export-t%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