[英]How to add two words to bash script variable
I'm trying to read two words (or more) into a variable $ans
. 我正在尝试将两个(或多个)单词读入变量
$ans
。 It works fine if I use a single word as answer, but it fails otherwise: 如果我使用单个单词作为答案,则效果很好,但否则失败:
echo "hello what is your name"
read ans
if [ $ans = "zarkon kootz" ]; then
echo "hello zarkon kootz"
elif [ $ans = "zorlac kootz" ]; then
echo "hello zorlac kootz"
else
echo "Hey $ans get off my computer"
fi
Any ideas on how to have more than a single word read into variable $ans
? 关于如何将多个单词读入变量
$ans
任何想法?
You were so close 你好亲近
The only thing you had to do was enclosing your variables in (double)quotes 您唯一要做的就是将变量括在(双引号)中
this is how you wrote it, I just added the quotes and it works: 这是您编写的方式,我只添加了引号即可使用:
echo "hello what is your name"
read ans
if [ "$ans" = "zarkon kootz" ]; then
echo "hello zarkon kootz"
elif [ "$ans" = "zorlac kootz" ]; then
echo "hello zorlac kootz"
else
echo "Hey $ans get off my computer"
fi
If there is more than one word in the input, each word can be assigned to a different variable. 如果输入中有多个单词,则可以将每个单词分配给一个不同的变量。
ie do 即做
read lastName firstName
instead of 代替
read ans
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.