bad substitution when creating nested array variables in bash

Viewed 50

I don't know what I am doing and could use some assistance with my script.

$ ./mysql.sh LOCALIP 'SELECT LOCALIP FROM Host'

mysql.sh

#!/bin/bash
source $PWD/data/login

mapfile -t "$1" < <(mysql -N ""$DB"" -h""$HOST"" -u""$USER"" -p""$PASS"" -se "$2")

echo ${$1[0]}
echo ${$1[1]}
echo ${$1[2]}
echo ${$1[3]}

fi

Output

[シ]owner@gwpi ~/scriptdir $./mysql.sh LOCALIP 'SELECT LOCALIP FROM Host'
./mysql.sh: line 10: ${$1[0]}: bad substitution

1 Answers

Simply replace the varible $1 with var here.It works.

$ mapfile -t var < <(mysql -N ""$DB"" -h""$HOST"" -u""$USER"" -p""$PASS"" -se "$2")
$ echo" ${var[@]}"

Original script has two problems.

  • You cannot change $1 arg in this way.Right style refers to this
  • should be better using more meaningful variables than $1
Related