2

I'm trying to expand variables Bash. I've searched but am not sure what the search terms are for what I am trying to do. Here is my code. Two questions:

  1. How do I get the desired output as shown?
  2. What is the Bash terminology to describe what I want here?
# Set Library install directories
install_dir_1=~/Desktop/arduino-1.8.4
install_dir_2=~/Desktop/arduino-1.8.5
install_dir_3=~/Desktop/arduino-1.8.5a

x=0

while [ $x -lt 4 ]; do

    let x=x+1 

    install_dir=install_dir_$x
    #echo $install_dir

    install_dir=$install_dir   
    #echo $install_dir

    dir_Adafruit_SSD1306=$install_dir/libraries/Adafruit_SSD1306/
    echo $dir_Adafruit_SSD1306

done

Actual output is

install_dir_1/libraries/Adafruit_SSD1306/
install_dir_2/libraries/Adafruit_SSD1306/
install_dir_3/libraries/Adafruit_SSD1306/
install_dir_4/libraries/Adafruit_SSD1306/

Desired output is

~/Desktop/arduino-1.8.4/libraries/Adafruit_SSD1306/
~/Desktop/arduino-1.8.5/libraries/Adafruit_SSD1306/
~/Desktop/arduino-1.8.5a/libraries/Adafruit_SSD1306/
4

It's called dereferencing variables and, while possible, is really not necessary here. So, yes, you could use the same approach and use dereferencing as shown below:

#!/bin/bash
# Set Library install directories
install_dir_1=~/Desktop/arduino-1.8.4
install_dir_2=~/Desktop/arduino-1.8.5
install_dir_3=~/Desktop/arduino-1.8.5a

x=1

while [ $x -lt 4 ]; do
    ## Set the instal_dir variable to point to the
    ## name of the variable you want to dereference
    install_dir="install_dir_$x"
    ## Dereference the variable using the ${!var} notation
    install_dir=${!install_dir}

    dir_Adafruit_SSD1306="$install_dir/libraries/Adafruit_SSD1306/"
    echo "$dir_Adafruit_SSD1306"
    let x=x+1 

done

But this is really way too complex. Here's a simplified version that does the same thing:

#!/bin/bash
install_dirs=("~/Desktop/arduino-1.8.4" 
              "~/Desktop/arduino-1.8.5" 
              "~/Desktop/arduino-1.8.5a")

for((x=0;x<${#install_dirs[@]};x++)); do 
    install_dir=${install_dirs[$x]}
    echo "$install_dir"
done
  • 2
    or `for install_dir in "${install_dirs[@]}" – ilkkachu Dec 8 '17 at 9:41
  • 2
    @ilkkachu yes, but I wanted to show an approach that uses the index numbers as in the original OP. I am assuming this is a simplified example and the indices are relevant. – terdon Dec 8 '17 at 9:42
  • Thanks @terdon. There are three directories listed in install_dirs=(....). How do I extract that number ie 3 to a variable and put it into 'for x in {0..2}; do', replacing the 2 with the variable minus 1? – Kes Dec 8 '17 at 10:31
  • 1
    @Kes first, as Ikkachu suggested, you might not need the variable at all: for dir in "${install_dirs[@]}". If you do need it, you can get the number of elements in the array with ${#install_dirs} but you can't use that in the {0..2} brace expansion. You will have to do something like for((x=0;x<${#install_dirs[@]};x++)); do echo ${install_dirs[$x]}; done – terdon Dec 8 '17 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.