Why when reading input with
read, and the input is
??? the result is
$ read ??? $ echo $REPLY bin src
bash on macOS.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
The data held in the variable
REPLY is still
???, but the result from using the variable unquoted with
echo, like you are doing, is the same as doing
You need to double quote all variable expansions.
When you leave a variable expansion unquoted, two things happens:
$IFS(a space, tab and a newline by default). In your case, the result of the splitting is the same as before the splitting (the single word
???) if the value of
$IFSis not modified.
???is, any filenames matching that pattern will replace the pattern. The pattern
???matches any three character long filename and you obviously have two of those in your current working directory.
Neither of these things happens if the variable expansion is double-quoted.
Example, recreating your issue and solving it:
$ ls X11R6 games lib libexec mdec ports share xobj bin include libdata local obj sbin src $ read ??? $ echo $REPLY bin lib obj src $ echo ??? bin lib obj src $ echo "$REPLY" ???
Another example in the same directory as above, showing that the string that I input is split into two words (
s*) and that these then gets used as filename globbing patterns:
$ read ??? s* $ echo $REPLY bin lib obj src sbin share src $ echo "$REPLY" ??? s*
src gets outputted twice as it matches both
Short summary of the above questions and answers: Always double quote all expansions, unless you know exactly which ones don't need it or if you actually want to invoke splitting and globbing.