I am trying to redirect the output of a command that contains couple of user input variables to an array. I first tried this script -
echo "Type the ACL name"
read acl
echo "Type the DATACENTER name"
read dc
echo "ACL is $acl, DC is $dc"
mkdir ~/$acl
device=($(grep -iEr $acl ~/sync-configs/$dc/configs/* | awk -F ':' {'print $1'} | awk -F '/' {'print $NF'} | sort | uniq))
The script created the ~/acl directory just fine but the grep did not work for me. Then, after doing some research, I adjusted the grep to put the variables in double quotes, like this -
echo "Type the ACL name"
read acl
echo "Type the DATACENTER name"
read dc
echo "ACL is $acl, DC is $dc"
mkdir ~/$acl
device=($(grep -iEr "$acl" ~/sync-configs/"$dc"/configs/* | awk -F ':' {'print $1'} | awk -F '/' {'print $NF'} | sort | uniq))
This seems to work fine and I am able to see elements in the device array.
My question is why do I need to put the $acl and $dc quotes when feeding them to the array but the command mkdir ~/$acl
doesn't require any quotes? Can some one provide clarity on this?
acl
anddc
, wouldn't it. – Satō Katsura May 13 '17 at 05:35declare
d integers too. – ilkkachu May 13 '17 at 10:23read
... Which makes for some amusing effects if the value fromread
happens to be the name of a variable. I was going to say something about integer variables being safe from word splitting, but then someone is going to setIFS=2
, so nevermind. – ilkkachu May 13 '17 at 10:51acl
var contains only a single word, the mkdir works as you expect. Try the same withacl
set to two words, sayfoo bar
. – ilkkachu May 13 '17 at 10:54typeset
anddeclare
except which absolutely necessary, e.g.typeset -A some_array
especially because different shells seem to use them differently – the_velour_fog May 13 '17 at 10:59