I'm trying to build a basic REPL in bash. The script dynamically populates a list of files in a directory for the user to run.
File space:
|
|\ scripts/
|| script1.sh
|| script2.sh
|
\ shell/
| shell.bashrc
| shell.desktop
From shell.bashrc, I'm using the following command to get an array of filenames:
readarray -d " " filenames < <(find ../bin -type f -executable)
I can print the array filenames
just fine and it contains a space separated string that holds "script1.sh script2.sh"
as expected.
But when I try to access the first element of the array with echo ${filenames[0]}
it prints every element. Any other index besides 0 returns an empty string.
My bash version is 5.0.17, and the first line of the file is #!/bin/bash
I moved to using "readarray" after trying the following led to similar results:
filenames=($(find "../bin" -type f -executable))
Edit: Found a dumb workaround and would still like to know where the original post is messing up. Workaround:
readarray -d " " filenames < <(find ../bin -type f -executable)
arr=($filenames)
echo ${arr[1]}
Which prints the 6th element of the array as expected.