gpt4 book ai didi

linux - 如何将两个单词添加到 bash 脚本变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:59:22 24 4
gpt4 key购买 nike

我正在尝试将两个单词(或更多)读入变量 $ans。如果我使用一个单词作为答案,它会工作正常,但否则会失败:

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

关于如何将多个单词读入变量 $ans 有什么想法吗?

最佳答案

你是那么亲密
您唯一需要做的就是用(双)引号将变量括起来

你是这样写的,我只是加了引号就可以了:

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

关于linux - 如何将两个单词添加到 bash 脚本变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20061800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com