2

So far I have found some examples of Bash select function usage with logical constructed options or Asterisk one (i.e select s in *). This last one lists all actual directory content.

So I would like to understand the following:

  1. What does * mean?

  2. How could I display another directory content?
    (I tried something as opt=$( ls path ) and select s in "$opt[@]", but it fails)

Any related links or examples are welcome.

artu-hnrq
  • 297

1 Answers1

2

The * is a so-called "glob" and will be expanded by the bash as "all files in the current directory", so it will basically expand to a space-separated list of all files in the current directory (which is the format commonly expected by many bash builtin flow-control constructs).

Globs can be used not only stand-alone but also as part of expressions, so assuming you have a directory stored in a bash variable $searchdir (be careful in naming bash variables in order not to accidentally overwrite essential environment variables such as $PATH) the construct

select s in "$searchdir"/*

will expand to "all files in $searchdir".

Note, however, that if the directory happens to be empty, the expression will amount to a literal * (or literally /the/actual/value/pointed/to/by/searchdir/*) unless you have set the nullglob option via

shopt -s nullglob

Then, if the directory is empty, the glob construct will expand to an empty list which is usually what you will want.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
AdminBee
  • 22,803