0

I have GitforWindows 2.20.1 installed in Windows 7 64Bit.

Now as I stated in the question, the multiple variable assignment syntax is not always working, especially when the value to be assigned is a command's output, i.e.:

read -r a b c <<< $(echo 1 2 3) ; echo "$a|$b|$c"

works, but these doesn't:

read -r SCRIPTDIR MAGE2ROOT <<< $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
## OR ##
read -r -d "\n" SCRIPTDIR MAGE2ROOT <<< $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

cause if I print the output just after this like:

echo $SCRIPTDIR && echo $MAGE2ROOT && exit

it just prints the path once. Why? And how can we make it work ?

Any help/guidance required.

Vicky Dev
  • 1,893
  • 2
  • 27
  • 62
  • 1
    What are you expecting `MAGE2ROOT` to be set to? `cd "$(dirname "${BASH_SOURCE[0]}")" && pwd` only prints one thing. – Gordon Davisson Feb 06 '19 at 01:11
  • That is a way to get the running script's directory, I want `SCRIPTDIR` & `MAGE2ROOT`, both to store that same value simultaneously... – Vicky Dev Feb 07 '19 at 15:37
  • 1
    But the command only prints one thing. Just set the second var afterwards: `MAGE2ROOT="$SCRIPTDIR"` – Olli K Feb 07 '19 at 16:08

1 Answers1

0

read doesn't do what you want it to. If it doesn't read as many "words" as there are variables to assign, it just leaves the extra variables blank. In this case, it's only getting one word (the path to the script's directory), but it has two variables to assign (SCRIPTDIR and MAGE2ROOT), so it sets SCRIPTDIR to the path, and MAGE2ROOT is left blank. Try your first example, but without supplying enough values to fill in all the variables:

$ read -r a b c <<< $(echo 1 2 3) ; echo "$a|$b|$c"
1|2|3
$ read -r a b c <<< $(echo 1 2) ; echo "$a|$b|$c"
1|2|
$ read -r a b c <<< $(echo 1) ; echo "$a|$b|$c"
1||

Fortunately, this is easy to solve (as @OlliK said) with MAGE2ROOT="$SCRIPTDIR". Actually, there's no point in using read at all, just use a plain assignment:

SCRIPTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
MAGE2ROOT="$SCRIPTDIR"
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151