Firstly, what you have isn't a regular expression. Expansions done by the shell involving *
are called Filename Expansions, also known as "glob" expansion.
Also you are not using an array to hold your contents, but using a scalar type. When you assign the glob expression to a variable is it assigned "literally" and when you do a unquoted expansion with echo
, the shell expands the variable first and then expands the glob to its resulting files. But remember that variables by default are of scalar type, they can contain only one value. For more than one value, you need arrays.
inputDir=/home/user/TestDir
files=( "$inputDir"/*txt)
printf '%s\n' "${files[@]}"
The list of elements is accessed with "${files[@]}"
(the double-quotes are important here), and individual elements accessed as "${files[0]}"
. The things with arrays though, you cease to be POSIX compliant as they are supported in those shells.
Also don't use uppercase letters for variable names in user scripts. The uppercase variable names have special meaning and are reserved for use by the shell only.