I have a bash script (list.sh) that lists files in a folder and assigns each filename to a bash variable:
#/bin/bash c=0 for file in *; do varr$c="$file"; c=$((c+1)); done
When I call this from a bash terminal with:
bash: varr0=07 Get the Balance Right!.mp3: command not found... bash: varr1=190731_10150450783260347_1948451_n.jpg: command not found... bash: varr2=199828_10150450907505347_7125763_n.jpg: command not found... bash: varr3=2022-07-31_19-30.png: command not found... bash: varr4=2022-08-02_12-06.png: command not found... bash: varr5=246915_10152020928305567_1284271814_n.jpg: command not found...
I don’t know how to put quotes around the file text itself, so that each varr(x) creates itself as a variable in the parent bash script, ie:
varr0="07 Get the Balance Right!.mp3"
It’s not a "quote around the text" issue, it’s the variable declaration
varr$c that’s not working. You should be using
This script solves your problem :
#/bin/bash c=0 for file in *; do declare varr$c=$file; c=$((c+1)); done
Answered By – bdelphin