I think it is reading the spaces as separate variables
It does not, as you can easily verify with this:
read -p 'Enter string:' x
echo "Entered: >>>$x<<<"
If you dislike quoting your variables (to avoid word splitting), you may consider switching from bash to Zsh. Where you have to write "$x"
in bash, you would simply write $x
in Zsh.
Hence, you would have to write
for file in "$directory"
but this would loop just one iteration, with file
bound to the content of the variable directory
. For looping over the entries in this directory, you would do a
for dirent in "$directory"/*
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…