I want to put the output of an ls into an array so I can loop through it and eventually use a entry the user will specify.
What I did is the following
SETUPS="$(ls)"
IFS=$' ' read -rd '' setup <<<"$SETUPS"
when I run echo $setup[0]
it will already show me all the files that are present
while I should be able to run echo $setup[0]
and only get the first entry.
Could anyone tell me what I'm doing wrong here?
I already tried SETUPS="$(ls -1)"
to seperate it with IFS=$'\n' read -rd '' setup <<<"$SETUPS"
but that didn't work either.
Right now I'm looping through it like this
n=0
for i in `echo ${setup} | tr "," " "`
do
n=$((n+1))
echo $n". $i"
done
which works to echo every entry with a number in front of it but I can't possibly select an entry out of there since every value seems to be stored as 1 value
If you want to get all the files in this directory in an array you can use globbing:
will store all the files matched by the glob in the array
files
and then you can print them withprintf
if you so desire, or do whatever else you want with looping over the array.You don't even need to store it in an array in the middle if you don't need it for some other purpose:
works just fine