Passing same value to more variables












1














I have written a script like this:



#!/bin/bash
set -e

dirs=()
dirs[0]=`export DIR_1=./dir1 && echo $DIR_1`
dirs[1]=`export DIR_2=./dir2 && echo $DIR_2`

for v in ${dirs[@]}; do

echo $v;
mkdir -p $v

done

# Here is my question: why it is null???
echo $DIR_1


How can I both do export the param and echo the value in same line?










share|improve this question
























  • ...something like export DIR_1=./dir1; echo $DIR_1?... (instead of echo you can use any statement)
    – linuxfan
    Nov 20 at 7:00












  • what do you mean "instead of echo"?
    – Zhao Rocky
    Nov 20 at 7:08






  • 1




    Are you running the echo $DIR_1 after running your script? In which case it will be null. If you want to persist the value of environment variables you set it a script to be available after the script exits then run it as ". <script name>" That is a dot followed by a space followed by the script file name.
    – Luv
    Nov 20 at 7:23










  • Its hard to see what you are trying to achieve here. DIR_1=./dir1;dirs[0]=$DIR_1;echo $DIR_1 saves you a whole subshell. What motivates the && - assigning a constant to a variable will never fail surely.
    – Niall Cosgrove
    Nov 20 at 7:30












  • @NiallCosgrove yes, 3 lines. that's what I mean "I wanna do it in one line" ;) I donnot know if that's possible though ;) just like "a=b=1 // a=1 also b =1"
    – Zhao Rocky
    Nov 20 at 7:35
















1














I have written a script like this:



#!/bin/bash
set -e

dirs=()
dirs[0]=`export DIR_1=./dir1 && echo $DIR_1`
dirs[1]=`export DIR_2=./dir2 && echo $DIR_2`

for v in ${dirs[@]}; do

echo $v;
mkdir -p $v

done

# Here is my question: why it is null???
echo $DIR_1


How can I both do export the param and echo the value in same line?










share|improve this question
























  • ...something like export DIR_1=./dir1; echo $DIR_1?... (instead of echo you can use any statement)
    – linuxfan
    Nov 20 at 7:00












  • what do you mean "instead of echo"?
    – Zhao Rocky
    Nov 20 at 7:08






  • 1




    Are you running the echo $DIR_1 after running your script? In which case it will be null. If you want to persist the value of environment variables you set it a script to be available after the script exits then run it as ". <script name>" That is a dot followed by a space followed by the script file name.
    – Luv
    Nov 20 at 7:23










  • Its hard to see what you are trying to achieve here. DIR_1=./dir1;dirs[0]=$DIR_1;echo $DIR_1 saves you a whole subshell. What motivates the && - assigning a constant to a variable will never fail surely.
    – Niall Cosgrove
    Nov 20 at 7:30












  • @NiallCosgrove yes, 3 lines. that's what I mean "I wanna do it in one line" ;) I donnot know if that's possible though ;) just like "a=b=1 // a=1 also b =1"
    – Zhao Rocky
    Nov 20 at 7:35














1












1








1







I have written a script like this:



#!/bin/bash
set -e

dirs=()
dirs[0]=`export DIR_1=./dir1 && echo $DIR_1`
dirs[1]=`export DIR_2=./dir2 && echo $DIR_2`

for v in ${dirs[@]}; do

echo $v;
mkdir -p $v

done

# Here is my question: why it is null???
echo $DIR_1


How can I both do export the param and echo the value in same line?










share|improve this question















I have written a script like this:



#!/bin/bash
set -e

dirs=()
dirs[0]=`export DIR_1=./dir1 && echo $DIR_1`
dirs[1]=`export DIR_2=./dir2 && echo $DIR_2`

for v in ${dirs[@]}; do

echo $v;
mkdir -p $v

done

# Here is my question: why it is null???
echo $DIR_1


How can I both do export the param and echo the value in same line?







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 13:21









Biffen

4,16352129




4,16352129










asked Nov 20 at 6:54









Zhao Rocky

66431128




66431128












  • ...something like export DIR_1=./dir1; echo $DIR_1?... (instead of echo you can use any statement)
    – linuxfan
    Nov 20 at 7:00












  • what do you mean "instead of echo"?
    – Zhao Rocky
    Nov 20 at 7:08






  • 1




    Are you running the echo $DIR_1 after running your script? In which case it will be null. If you want to persist the value of environment variables you set it a script to be available after the script exits then run it as ". <script name>" That is a dot followed by a space followed by the script file name.
    – Luv
    Nov 20 at 7:23










  • Its hard to see what you are trying to achieve here. DIR_1=./dir1;dirs[0]=$DIR_1;echo $DIR_1 saves you a whole subshell. What motivates the && - assigning a constant to a variable will never fail surely.
    – Niall Cosgrove
    Nov 20 at 7:30












  • @NiallCosgrove yes, 3 lines. that's what I mean "I wanna do it in one line" ;) I donnot know if that's possible though ;) just like "a=b=1 // a=1 also b =1"
    – Zhao Rocky
    Nov 20 at 7:35


















  • ...something like export DIR_1=./dir1; echo $DIR_1?... (instead of echo you can use any statement)
    – linuxfan
    Nov 20 at 7:00












  • what do you mean "instead of echo"?
    – Zhao Rocky
    Nov 20 at 7:08






  • 1




    Are you running the echo $DIR_1 after running your script? In which case it will be null. If you want to persist the value of environment variables you set it a script to be available after the script exits then run it as ". <script name>" That is a dot followed by a space followed by the script file name.
    – Luv
    Nov 20 at 7:23










  • Its hard to see what you are trying to achieve here. DIR_1=./dir1;dirs[0]=$DIR_1;echo $DIR_1 saves you a whole subshell. What motivates the && - assigning a constant to a variable will never fail surely.
    – Niall Cosgrove
    Nov 20 at 7:30












  • @NiallCosgrove yes, 3 lines. that's what I mean "I wanna do it in one line" ;) I donnot know if that's possible though ;) just like "a=b=1 // a=1 also b =1"
    – Zhao Rocky
    Nov 20 at 7:35
















...something like export DIR_1=./dir1; echo $DIR_1?... (instead of echo you can use any statement)
– linuxfan
Nov 20 at 7:00






...something like export DIR_1=./dir1; echo $DIR_1?... (instead of echo you can use any statement)
– linuxfan
Nov 20 at 7:00














what do you mean "instead of echo"?
– Zhao Rocky
Nov 20 at 7:08




what do you mean "instead of echo"?
– Zhao Rocky
Nov 20 at 7:08




1




1




Are you running the echo $DIR_1 after running your script? In which case it will be null. If you want to persist the value of environment variables you set it a script to be available after the script exits then run it as ". <script name>" That is a dot followed by a space followed by the script file name.
– Luv
Nov 20 at 7:23




Are you running the echo $DIR_1 after running your script? In which case it will be null. If you want to persist the value of environment variables you set it a script to be available after the script exits then run it as ". <script name>" That is a dot followed by a space followed by the script file name.
– Luv
Nov 20 at 7:23












Its hard to see what you are trying to achieve here. DIR_1=./dir1;dirs[0]=$DIR_1;echo $DIR_1 saves you a whole subshell. What motivates the && - assigning a constant to a variable will never fail surely.
– Niall Cosgrove
Nov 20 at 7:30






Its hard to see what you are trying to achieve here. DIR_1=./dir1;dirs[0]=$DIR_1;echo $DIR_1 saves you a whole subshell. What motivates the && - assigning a constant to a variable will never fail surely.
– Niall Cosgrove
Nov 20 at 7:30














@NiallCosgrove yes, 3 lines. that's what I mean "I wanna do it in one line" ;) I donnot know if that's possible though ;) just like "a=b=1 // a=1 also b =1"
– Zhao Rocky
Nov 20 at 7:35




@NiallCosgrove yes, 3 lines. that's what I mean "I wanna do it in one line" ;) I donnot know if that's possible though ;) just like "a=b=1 // a=1 also b =1"
– Zhao Rocky
Nov 20 at 7:35












2 Answers
2






active

oldest

votes


















1














The "<<<" operator in bash is called a here string.



If you want to do multiple assignments on a single line use read as follows



read -r var1 var2 var3 <<< $(echo val1 val2 val3)


Each word in the string you echo will get assigned to each variable.






share|improve this answer





















  • I accept it. Though I have to write the same value twice
    – Zhao Rocky
    Nov 20 at 7:59












  • @ZhaoRocky Yes that's true, but I cant think of a language where you can say a,b=1 - though I'm willing to be contradicted if someone knows of one.
    – Niall Cosgrove
    Nov 20 at 8:02












  • lol.................
    – Zhao Rocky
    Nov 20 at 8:05



















1














After my comment, I post this reply which explains what I meant with "instead of echo..."



The original line of code is:



dirs[0]=`export DIR_1=./dir1 && echo $DIR_1`


Here, the export does not work because it is executed under another instance of the shell, as you used the back ticks. In the child shell the export is executed, but the modified environment can not be "ported back".



Instead, to obtain the same, but working, result, you can write:



export DIR_1=./dir1;  dirs[0]=$(echo $DIR_1)


The above line is equivalent to your original one, it makes exactly the same thing, included the strange way to assign to dir[0], but the export is executed in the local shell and exported to all the future childs. What I meant was you can write any command after the "export DIR_1...", even dirs[0]=...






share|improve this answer























  • @Biffen thank you for editing, these backticks drive me crazy with my keyboard!
    – linuxfan
    Nov 21 at 13:33










  • I probably should have talked about this in my own answer. Good work :)
    – Niall Cosgrove
    Nov 21 at 13:53












  • @linuxfan thanks a lot for making that clear. I agree with you. Just for fun to know if any possible in one expression ;)
    – Zhao Rocky
    Nov 22 at 7:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53387711%2fpassing-same-value-to-more-variables%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









1














The "<<<" operator in bash is called a here string.



If you want to do multiple assignments on a single line use read as follows



read -r var1 var2 var3 <<< $(echo val1 val2 val3)


Each word in the string you echo will get assigned to each variable.






share|improve this answer





















  • I accept it. Though I have to write the same value twice
    – Zhao Rocky
    Nov 20 at 7:59












  • @ZhaoRocky Yes that's true, but I cant think of a language where you can say a,b=1 - though I'm willing to be contradicted if someone knows of one.
    – Niall Cosgrove
    Nov 20 at 8:02












  • lol.................
    – Zhao Rocky
    Nov 20 at 8:05
















1














The "<<<" operator in bash is called a here string.



If you want to do multiple assignments on a single line use read as follows



read -r var1 var2 var3 <<< $(echo val1 val2 val3)


Each word in the string you echo will get assigned to each variable.






share|improve this answer





















  • I accept it. Though I have to write the same value twice
    – Zhao Rocky
    Nov 20 at 7:59












  • @ZhaoRocky Yes that's true, but I cant think of a language where you can say a,b=1 - though I'm willing to be contradicted if someone knows of one.
    – Niall Cosgrove
    Nov 20 at 8:02












  • lol.................
    – Zhao Rocky
    Nov 20 at 8:05














1












1








1






The "<<<" operator in bash is called a here string.



If you want to do multiple assignments on a single line use read as follows



read -r var1 var2 var3 <<< $(echo val1 val2 val3)


Each word in the string you echo will get assigned to each variable.






share|improve this answer












The "<<<" operator in bash is called a here string.



If you want to do multiple assignments on a single line use read as follows



read -r var1 var2 var3 <<< $(echo val1 val2 val3)


Each word in the string you echo will get assigned to each variable.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 7:57









Niall Cosgrove

1,1681922




1,1681922












  • I accept it. Though I have to write the same value twice
    – Zhao Rocky
    Nov 20 at 7:59












  • @ZhaoRocky Yes that's true, but I cant think of a language where you can say a,b=1 - though I'm willing to be contradicted if someone knows of one.
    – Niall Cosgrove
    Nov 20 at 8:02












  • lol.................
    – Zhao Rocky
    Nov 20 at 8:05


















  • I accept it. Though I have to write the same value twice
    – Zhao Rocky
    Nov 20 at 7:59












  • @ZhaoRocky Yes that's true, but I cant think of a language where you can say a,b=1 - though I'm willing to be contradicted if someone knows of one.
    – Niall Cosgrove
    Nov 20 at 8:02












  • lol.................
    – Zhao Rocky
    Nov 20 at 8:05
















I accept it. Though I have to write the same value twice
– Zhao Rocky
Nov 20 at 7:59






I accept it. Though I have to write the same value twice
– Zhao Rocky
Nov 20 at 7:59














@ZhaoRocky Yes that's true, but I cant think of a language where you can say a,b=1 - though I'm willing to be contradicted if someone knows of one.
– Niall Cosgrove
Nov 20 at 8:02






@ZhaoRocky Yes that's true, but I cant think of a language where you can say a,b=1 - though I'm willing to be contradicted if someone knows of one.
– Niall Cosgrove
Nov 20 at 8:02














lol.................
– Zhao Rocky
Nov 20 at 8:05




lol.................
– Zhao Rocky
Nov 20 at 8:05













1














After my comment, I post this reply which explains what I meant with "instead of echo..."



The original line of code is:



dirs[0]=`export DIR_1=./dir1 && echo $DIR_1`


Here, the export does not work because it is executed under another instance of the shell, as you used the back ticks. In the child shell the export is executed, but the modified environment can not be "ported back".



Instead, to obtain the same, but working, result, you can write:



export DIR_1=./dir1;  dirs[0]=$(echo $DIR_1)


The above line is equivalent to your original one, it makes exactly the same thing, included the strange way to assign to dir[0], but the export is executed in the local shell and exported to all the future childs. What I meant was you can write any command after the "export DIR_1...", even dirs[0]=...






share|improve this answer























  • @Biffen thank you for editing, these backticks drive me crazy with my keyboard!
    – linuxfan
    Nov 21 at 13:33










  • I probably should have talked about this in my own answer. Good work :)
    – Niall Cosgrove
    Nov 21 at 13:53












  • @linuxfan thanks a lot for making that clear. I agree with you. Just for fun to know if any possible in one expression ;)
    – Zhao Rocky
    Nov 22 at 7:56
















1














After my comment, I post this reply which explains what I meant with "instead of echo..."



The original line of code is:



dirs[0]=`export DIR_1=./dir1 && echo $DIR_1`


Here, the export does not work because it is executed under another instance of the shell, as you used the back ticks. In the child shell the export is executed, but the modified environment can not be "ported back".



Instead, to obtain the same, but working, result, you can write:



export DIR_1=./dir1;  dirs[0]=$(echo $DIR_1)


The above line is equivalent to your original one, it makes exactly the same thing, included the strange way to assign to dir[0], but the export is executed in the local shell and exported to all the future childs. What I meant was you can write any command after the "export DIR_1...", even dirs[0]=...






share|improve this answer























  • @Biffen thank you for editing, these backticks drive me crazy with my keyboard!
    – linuxfan
    Nov 21 at 13:33










  • I probably should have talked about this in my own answer. Good work :)
    – Niall Cosgrove
    Nov 21 at 13:53












  • @linuxfan thanks a lot for making that clear. I agree with you. Just for fun to know if any possible in one expression ;)
    – Zhao Rocky
    Nov 22 at 7:56














1












1








1






After my comment, I post this reply which explains what I meant with "instead of echo..."



The original line of code is:



dirs[0]=`export DIR_1=./dir1 && echo $DIR_1`


Here, the export does not work because it is executed under another instance of the shell, as you used the back ticks. In the child shell the export is executed, but the modified environment can not be "ported back".



Instead, to obtain the same, but working, result, you can write:



export DIR_1=./dir1;  dirs[0]=$(echo $DIR_1)


The above line is equivalent to your original one, it makes exactly the same thing, included the strange way to assign to dir[0], but the export is executed in the local shell and exported to all the future childs. What I meant was you can write any command after the "export DIR_1...", even dirs[0]=...






share|improve this answer














After my comment, I post this reply which explains what I meant with "instead of echo..."



The original line of code is:



dirs[0]=`export DIR_1=./dir1 && echo $DIR_1`


Here, the export does not work because it is executed under another instance of the shell, as you used the back ticks. In the child shell the export is executed, but the modified environment can not be "ported back".



Instead, to obtain the same, but working, result, you can write:



export DIR_1=./dir1;  dirs[0]=$(echo $DIR_1)


The above line is equivalent to your original one, it makes exactly the same thing, included the strange way to assign to dir[0], but the export is executed in the local shell and exported to all the future childs. What I meant was you can write any command after the "export DIR_1...", even dirs[0]=...







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 13:22









Biffen

4,16352129




4,16352129










answered Nov 21 at 12:06









linuxfan

2,0722719




2,0722719












  • @Biffen thank you for editing, these backticks drive me crazy with my keyboard!
    – linuxfan
    Nov 21 at 13:33










  • I probably should have talked about this in my own answer. Good work :)
    – Niall Cosgrove
    Nov 21 at 13:53












  • @linuxfan thanks a lot for making that clear. I agree with you. Just for fun to know if any possible in one expression ;)
    – Zhao Rocky
    Nov 22 at 7:56


















  • @Biffen thank you for editing, these backticks drive me crazy with my keyboard!
    – linuxfan
    Nov 21 at 13:33










  • I probably should have talked about this in my own answer. Good work :)
    – Niall Cosgrove
    Nov 21 at 13:53












  • @linuxfan thanks a lot for making that clear. I agree with you. Just for fun to know if any possible in one expression ;)
    – Zhao Rocky
    Nov 22 at 7:56
















@Biffen thank you for editing, these backticks drive me crazy with my keyboard!
– linuxfan
Nov 21 at 13:33




@Biffen thank you for editing, these backticks drive me crazy with my keyboard!
– linuxfan
Nov 21 at 13:33












I probably should have talked about this in my own answer. Good work :)
– Niall Cosgrove
Nov 21 at 13:53






I probably should have talked about this in my own answer. Good work :)
– Niall Cosgrove
Nov 21 at 13:53














@linuxfan thanks a lot for making that clear. I agree with you. Just for fun to know if any possible in one expression ;)
– Zhao Rocky
Nov 22 at 7:56




@linuxfan thanks a lot for making that clear. I agree with you. Just for fun to know if any possible in one expression ;)
– Zhao Rocky
Nov 22 at 7:56


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f53387711%2fpassing-same-value-to-more-variables%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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?