【问题标题】:In Makefiles, Is there a way to print the dollar sign ($) using @echo or $(info )?在 Makefiles 中,有没有办法使用 @echo 或 $(info) 打印美元符号 ($)?
【发布时间】:2021-12-10 17:11:54
【问题描述】:

我一直在尝试使用 Makefile 中使用的 @echo$(info ) 将美元符号 ($) 打印到标准输出,但没有成功。

我可以在命令行的 bash shell 中使用echo 命令来打印 $ 符号而不会出现任何问题。但是当从 Makefile 中执行时,相同的 bash 命令无法按预期工作。

以下是我的测试Makefile:

noecho:
    $(info Print the dollar sign using info: $)
    @echo You only see this!  You DO NOT see the echo command printed above.
    @echo Remember echo is a shell command. 
    @echo You may need to escape special characters, like \\
    @echo 'Or put put text in single quotes! No need to escape special characters like \.'
    @echo 
    @echo "Enclosing characters in single quotes (') preserves the literal value of "
    @echo 'each character within the quotes. A single quote may not occur between '
    @echo 'single quotes, even when preceded by a backslash.'
    @echo
    @echo "Double quotes can also be used in most cases."
    @echo "Enclosing characters in double quotes (\") preserves the literal value of "
    @echo "all characters within the quotes, with the exception of $, \`, \\, and, "
    @echo "when history expansion is enabled, !"
    @echo 'Printing the $'

当我执行make noecho 时,我得到:

rob$ make noecho
Print the dollar sign using info: 
You only see this! You DO NOT see the echo command printed above.
Remember echo is a shell command.
You may need to escape special characters, like \
Or put put text in single quotes! No need to escape special characters like \.

Enclosing characters in single quotes (') preserves the literal value of 
each character within the quotes. A single quote may not occur between 
single quotes, even when preceded by a backslash.

Double quotes can also be used in most cases.
Enclosing characters in double quotes (") preserves the literal value of 
all characters within the quotes, with the exception of  `, \, and, 
when history expansion is enabled, !
/opt/local/bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/opt/local/bin/bash: -c: line 2: syntax error: unexpected end of file
make: *** [noecho] Error 2

我也尝试了@echo "'$'"@echo "\$",但没有成功。

有没有办法在 Makefile 中使用 @echo$(info ) 打印美元符号 ($)?

【问题讨论】:

  • 这是 make 所做的,而不是 bash 所做的(即使不是这样,makefile 的解释默认情况下根本不使用 bash;make 调用的默认 shell 是 sh )。

标签: bash makefile gnu-make


【解决方案1】:

将美元符号加倍以阻止 make 对其进行解释

echo $$
$(info $$)

【讨论】:

  • 谢谢。那行得通!我知道必须有一个简单的解决方案。
  • 我还刚刚发现,要使用 echo 或 @echo 将美元符号 $$ 打印两次,必须使用 $$\$$,除非美元符号在 $$$$ 起作用的地方用单引号括起来。如果您只是在 echo 或 @echo 命令中使用 $$$$ 而没有将美元符号括在单引号中,则 echo 和 @echo 会打印出一个奇怪的数字。谁能解释为什么会这样?
  • $$$$ 变成了$$ 这是一个包含当前PID的特殊shell变量。
  • 再次感谢!我忘了 $$ 是一个特殊的 shell 变量,代表进程的 PID。
  • $$ 是一个特殊的 shell 变量并不重要……重要的是 $ 对于 shell 来说是特殊的。因此,让 make 转义 $ 以将其传递给 shell 的方法是 $$。但是让 shell 转义 $ 以将其传递给它调用的程序的方法是使用单引号或反斜杠。基本上:首先让你的命令行从你的 shell 提示符下工作。一旦成功,将其放入您的 makefile 配方并将所有 $ 更改为 $$,它也将在那里工作。
猜你喜欢
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 2021-12-24
  • 2011-11-11
  • 2014-03-20
相关资源
最近更新 更多