Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
981 views
in Technique[技术] by (71.8m points)

read input in bash scrip that is a directory with spaces in the path


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"/*

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...