【问题标题】:Echo in Bash: Escape Apostrophes?Bash 中的回声:转义撇号?
【发布时间】:2015-09-13 15:49:19
【问题描述】:

我正在尝试在引号内的 bash 中使用 echo。 当我从命令行尝试时,它工作正常。

例如:echo "I'm testing the apostrophe functionality." 产生I'm testing the apostrophe functionality.

然而,当我在脚本中写这个时,它似乎不起作用。

这是我的代码的 sn-p:(我正在尝试将 ASCII 艺术集成到我的程序中)

if [ "$2" == "-s" ]

  then echo "   ___                           __ _             _            "
  echo "  / _ \__ _ _ __ ___   ___      / _\ |_ __ _ _ __| |_ ___ _ __ "
  echo " / /_\/ _` | '_ ` _ \ / _ \_____\ \| __/ _` | '__| __/ _ \ '__|"
  echo "/ /_\\ (_| | | | | | |  __/_____|\ \ || (_| | |  | ||  __/ |   "
  echo "\____/\__,_|_| |_| |_|\___|     \__/\__\__,_|_|   \__\___|_|   "
  echo ""                                                              
  echo "Hello!  My name is Siri."
  echo "I'm not actually the Siri you're probably used to."
  echo "I'm actually Apple's Siri's sister, the no-voice one."
  echo "Sorry, but I'm in development right now."
  echo "Come back later and maybe Eric will bring me out of beta."
  echo "Thanks for reading this long debug message!"
fi

我已经检查并仔细检查了我所有的报价......

但它仍然产生:

./game-starter.sh: line 7: unexpected EOF while looking for matching ``'
./game-starter.sh: line 88: syntax error: unexpected end of file

请尽快帮忙!

-HewwoCraziness

【问题讨论】:

    标签: bash escaping echo apostrophe


    【解决方案1】:

    当您在字符串周围使用双引号时,shell 会解释某些字符。一个例子是反引号,如Ryan's answer 中所述。

    一种选择是在您的字符串周围使用单引号,但您必须转义消息中的撇号。我认为最好的解决方案是改用heredoc:

    cat <<'EOF'
       ___                           __ _             _            
      / _ \__ _ _ __ ___   ___      / _\ |_ __ _ _ __| |_ ___ _ __ 
     / /_\/ _` | '_ ` _ \ / _ \_____\ \| __/ _` | '__| __/ _ \ '__|
    / /_\\ (_| | | | | | |  __/_____|\ \ || (_| | |  | ||  __/ |   
    \____/\__,_|_| |_| |_|\___|     \__/\__\__,_|_|   \__\___|_|   
    
    Hello!  My name is Siri.
    I'm not actually the Siri you're probably used to.
    I'm actually Apple's Siri's sister, the no-voice one.
    Sorry, but I'm in development right now.
    Come back later and maybe Eric will bring me out of beta.
    Thanks for reading this long debug message!
    EOF
    

    EOF 周围的引号表示字符串按字面意思解释,因此| 等字符不会引起问题。

    【讨论】:

    • 谢谢!我最终使用了一个简单的查找和替换脚本来解决我的问题。正如 Ryan 的回答一样,是的,我的 Figlet 文本是问题所在。谢谢你们!
    【解决方案2】:

    我不认为是撇号导致了您的问题;是 ` 字符吗(你知道,在 ~ 键上)。它用于就地运行命令和其他事情,如果我不得不根据该错误消息进行猜测,这可能是导致问题的原因。

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2016-03-23
      • 2018-08-26
      • 2013-03-22
      • 2018-09-21
      相关资源
      最近更新 更多