-1

I'm trying to show to the user the list of zip file contained in a folder and give user the ability to choice which file to elaborate.

i'm trying this

roms=$(ls ~/roms/*.zip)

PS3="Choose a ROM "
select opt in "${roms[@]}" "quit"; do
....
done 

The problem is that my menu is showed in this way:

1) /home/realtebo/roms/rom_01.zip
/home/realtebo/roms/rom_02.zip
/home/realtebo/roms/rom_02_v2.zip
....
2) quit

Instead I need this

1) /home/realtebo/roms/rom_01.zip
2) /home/realtebo/roms/rom_02.zip
3) /home/realtebo/roms/rom_02_v2.zip
...
n+1) quit

How to 'explode' the result of ls as an array? I'm using bash under linux mint 17.3

msw
  • 42,753
  • 9
  • 87
  • 112
realtebo
  • 23,922
  • 37
  • 112
  • 189
  • 1
    Possible duplicate of [Split string into an array in Bash](http://stackoverflow.com/questions/10586153/split-string-into-an-array-in-bash) – msw Mar 03 '16 at 12:26
  • `ls` is redundant *and* [wrong](http://mywiki.wooledge.org/ParsingLs) here. Use `roms=( ~/roms/*.zip )`. – chepner Mar 03 '16 at 12:28

1 Answers1

3

You don't really need a variable (and using ls to populate the variable is certainly discouraged -- see http://mywiki.wooledge.org/ParsingLs) so the code can be significantly simplified to

PS3="Choose a ROM "
select opt in ~/roms/*.zip "quit"; do
   :

If you want the file names in an array, just use the wildcard instead of ls (again, see the link above about why using ls breaks things):

roms=(~/roms/*.zip)
select opt in "${roms[@]}" "quit"; do
  :
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Yes, thank you a lot, this works, but .... my question was different. I'm really interessed in how to split an ls string as an array to be used with select – realtebo Mar 03 '16 at 12:22
  • @realtebo Note that your problem is that `rom=$(...)` doesn't create an array; it creates a flat string of space-separated filenames. – chepner Mar 03 '16 at 12:30