As an optional part of an assignment, I am making an interactive menu using select and have it support file names and directory names with spaces. The point is so the user can select which file or directory to interact with using a given function, decided earlier in the interactive menu. I have to use select as part of the requirements. In this case, I am trying to use cat to get the stored information of the file.

this doesn't work however

shopt -s nullglob
shopt -u nullglob
PS3= "Please select a file"
select SHOLDER in "${FARRAY[@]}"
   if  test -d "${FARRAY[$SHOLDER]}"
       echo "Please select a file"
       cat "${FARRAY[$SHOLDER]}"

I get the error: syntax error in expression (error token is "there"). hello there is the file name I have been testing

If I add "!" in front of the array name in the select loop and cat portion "${!FARRAY[@]}" it works but only shows the user the array indexes rather than the file names, which isn't really usable. I was wondering why this is the case and what steps I need to take to make my script perform this section properly.

  • 2
    (1) The script is clumsily typed: missing quote, FSHOLDER out of the blue, FARRAY= (*) should be FARRAY=(*) etc. Therefore the code you posted needs to be debugged before it fails in the way you described. (2) When it finally fails in the way you described, place echo "$SHOLDER" after do and be surprised. Dec 12, 2021 at 0:34

1 Answer 1


From help select:

select: select NAME [in WORDS ... ;] do COMMANDS; done

[...] If the line consists of the number corresponding to one of the displayed words, then NAME is set to that word.


$ select foo in abc def; do echo "foo is '$foo'"; break; done
1) abc
2) def
#? 2
foo is 'def'

That is to say if you pick a file called foo.c, then SHOLDER gets set to foo.c (not the number of file), and that's going to produce an arithmetic error when the array is indexed.

You'd probably also have issues from the fact that select numbers the choices from one, while Bash arrays are indexed from zero.

It's probably better to copy and paste your code to the question here, that would avoid typos from confusing the matter. Like the spaces in your assignments here, FARRAY= (*) would give a syntax error.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .