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?

6
  • ...something like export DIR_1=./dir1; echo $DIR_1?... (instead of echo you can use any statement) Commented Nov 20, 2018 at 7:00
  • what do you mean "instead of echo"? Commented Nov 20, 2018 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. Commented Nov 20, 2018 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. Commented Nov 20, 2018 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" Commented Nov 20, 2018 at 7:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I accept it. Though I have to write the same value twice
@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.
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]=...

3 Comments

@Biffen thank you for editing, these backticks drive me crazy with my keyboard!
I probably should have talked about this in my own answer. Good work :)
@linuxfan thanks a lot for making that clear. I agree with you. Just for fun to know if any possible in one expression ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.