Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用bash read获取完整的用户输入_Bash - Fatal编程技术网

使用bash read获取完整的用户输入

使用bash read获取完整的用户输入,bash,Bash,简单的问题 我的脚本要求用户输入,然后尝试使用该输入做一些事情。但是当使用read命令时,我只得到用户输入的第一个单词(由空格分隔)。无论我是否用引号输入响应,似乎都会发生这种情况。最好不要要求用户在报价单中键入他们的消息 代码如下: echo "what is your commit message?" read commitmessage 如果输入:这是自动提交或“这是自动提交” Bash只将$commitmessage理解为“This” 感谢您的帮助。问题不在阅读上:我打赌您在使用变量时

简单的问题

我的脚本要求用户输入,然后尝试使用该输入做一些事情。但是当使用
read
命令时,我只得到用户输入的第一个单词(由空格分隔)。无论我是否用引号输入响应,似乎都会发生这种情况。最好不要要求用户在报价单中键入他们的消息

代码如下:

echo "what is your commit message?"
read commitmessage
如果输入:这是自动提交或“这是自动提交”

Bash只将
$commitmessage
理解为
“This”


感谢您的帮助。

问题不在阅读
上:我打赌您在使用变量时没有引用它

$ read -p "message? " msg
message? hello world this is a message
$ echo "$msg"
hello world this is a message
使用变量时,请确保引用它:
“$msg”

否则,shell将在空格(或$IFS中的任何内容)上拆分值

例如:

$ function first_word { echo "$1"; }
$ first_word "$msg"
hello world this is a message
$ first_word $msg
hello

$commitmessage
应包含整个字符串。你怎么查到的?
echo“$commitmessage”
之后的输出是什么?(而且您不需要引号(事实上,您不希望在用户输入中使用引号,除非它们是消息本身的一部分)。
IFS=
只会防止分词和丢失额外的空格(因此可能不是坏事)但是在这种情况下,实际上不会改变是否将多个单词分配给
commitmessage
。您能否澄清如何使用IFS=请参阅搜索IFS.Gotcha。我刚刚在函数开头准确地编写了
$IFS=
,现在它可以工作了。谢谢