在 shell 脚本中读取空间作为输入

Kum*_*mar 6 shell quoting read

如何" "在 shell 脚本中提供空间作为输入?

前任 :

echo " Enter date for grep... Ex: Oct  6 [***Double space is important for single date***] (or) Oct 12 "
read mdate 
echo $mdate
Run Code Online (Sandbox Code Playgroud)

我得到的输出为Oct 6但我想要Oct 6

Gil*_*il' 10

您已经有了Oct  6in $mdate,您的问题是您在打印时正在扩展它。始终在变量替换周围使用双引号。此外,要保留前导和尾随空格(那些被 剥离的read),请设置IFS为空字符串。

IFS= read -r mdate
echo "$mdate"
Run Code Online (Sandbox Code Playgroud)

read的-r( r aw) 选项告诉它不要特别对待反斜杠,这是您大多数时候想要的(但这不是这里的问题)。