【问题标题】:Bash Script Echoing double quotes when I don't want it too当我不想要它时,Bash Script Echoing 双引号
【发布时间】:2014-03-29 14:57:42
【问题描述】:

我不知道这里发生了什么。我制作的 bash 脚本超过了我的份额。搜索过去的问题只是让人们询问如何使引号持久。

当回显到 /dev/tty 时,引号也会回显。

例子:

#!/bin/sh
OLDIFS=$IFS
IFS=$'\n'
currentfile=This.File;
echo 'About to output' > /dev/tty;
echo “The Currect File is $currentfile” > /dev/tty;
IFS=$OLDIFS

会回显:

\#'About to output'
\#"The Currect File is ??

如果我在 $currectfile 后面加一个空格,我会得到更正确但不是我想要的:

\#'About to output'
\#"The Currect File is This.File "

我期待的输出很简单:

\#About to output
\#The Currect File is This.File

在交互式输入此行时,它会按预期回显。

【问题讨论】:

  • 当我只是在做一个例子的时候,我执行了我的例子程序并得到了 Macbook-Pro-15 的输出:Desktop user$ bash anotherDirectory/test.sh 即将输出“当前文件是? ? Macbook-Pro-15:Desktop user$ sh anotherDirectory/test.sh 即将输出“The Currect File is ??因此,虽然单引号在这里消失了但出现在我的主程序中,但双引号是相同的。
  • 您的代码中似乎有 U+201C(左双引号)和 U+201D(右双引号),而不是 U+0022(引号)。
  • 正确!我只需要改变它们。谢谢
  • 你所说的改变是什么意思?您是否正在寻找将这些 un​​icode 引号替换为常规 " 的代码

标签: bash echo quotes


【解决方案1】:

让我们通过shellcheck 运行您的代码以自动检查常见问题:

echo “The Currect File is $currentfile” > /dev/tty;
     ^-- SC1015: This is a unicode double quote. Delete and retype it.

让我们这样做:

echo "The Currect File is $currentfile" > /dev/tty;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    相关资源
    最近更新 更多