在混合的Bash-Python代码片段中,变量的双引号和单引号

时间:2022-09-15 13:37:24

I am completely stuck at mixing Python with Bash and it is really tough on me.
I know that using double vs single quotes in Bash depends on your intention to use variables, escapes and special characters.
I also know, that using double vs single quotes in Python is matter of taste mostly and PEP8 also confirms this.

我完全沉迷于将Python与Bash混合,这对我来说非常困难。我知道在Bash中使用双引号和单引号取决于您使用变量、转义和特殊字符的意图。我也知道,在Python中使用双引号和单引号最重要的是味道,而PEP8也证实了这一点。

But what if I want to use Python snippet inside Bash like this? It works either way.

但是,如果我想在Bash中使用Python代码片段,该怎么办呢?它的工作原理。

#!/bin/bash
arg3="something special"
python << END
import functions; 
print functions.do_smth('$arg', '$arg2')    
print len("$arg3");
END

Does Python or Bash rules apply for the quotes in the above snippet?

Python或Bash规则是否适用于上述代码片段中的引号?

Is it different from such allocation of Python code?

它与Python代码的这种分配不同吗?

python -c "import functions; print functions.do_smth('$arg', '$arg2'); print len("$arg3");"

Does it matter for quoting when I use 1st or 2nd variant? Does it matter if I pass variable to function or simply use it?

当我使用第1或第2个变体时,引用它有关系吗?如果我把变量传递给函数或者简单地使用它,这有关系吗?

python -c "print 'great program eh'"
python -c "import special; do_smth_special_with('great program eh')"

A have completely no idea of the differences between all these use-cases!
Especially many problems I face when I should pass JSON output as variable (which also has goddamn quotes and escapes!) from Bash to Python function.

A完全不知道所有这些用例之间的区别!特别是当我将JSON输出作为变量(也有该死的引号和转义)从Bash传递到Python函数时,我面临的问题尤其多。

Really need thorough explanation here.

这里需要充分的说明。

1 个解决方案

#1


1  

Does Python or Bash rules apply for the quotes in [an unquoted heredoc]?

Python或Bash规则是否适用于[未引用的heredoc]中的引号?

Python rules apply to the quotes; bash rules apply to the $ sigils. Since bash never looks at the quotes, it doesn't matter to bash what type of quotes they are; the $foo gets replaced regardless (unless you use \$; bash looks at backslashes).

Python规则适用于引号;bash规则适用于$ sigils。由于bash从不查看引号,因此抨击它们是什么类型的引号并不重要;$foo无论如何都会被替换(除非您使用\$;bash看着反斜杠)。

If you'd quoted the heredoc delimiter (<<<"END"), then bash would not attempt to replace variable expansions nor interpret backslashes.

如果您引用了heredoc delimiter (<<<"END"),那么bash将不会尝试替换变量扩展,也不会试图解释反斜杠。

The rules for heredocs are explained thoroughly in the bash manual.

本教程的规则在bash手册中有详细的说明。

Is it different from [python -c "…"]?

它与[python -c "…]不同吗?

Yes, bash interprets command-line arguments before passing them to the program, so bash rules apply and python only sees whatever quotes which were passed through.

是的,bash会在将命令行参数传递给程序之前对它们进行解释,因此,bash规则应用程序和python只会看到经过的任何引号。

The rules for words in the command line are explained thoroughly in the bash manual (read this section in context).

命令行中单词的规则在bash手册中得到了详细的解释(请在上下文中阅读本节)。

#1


1  

Does Python or Bash rules apply for the quotes in [an unquoted heredoc]?

Python或Bash规则是否适用于[未引用的heredoc]中的引号?

Python rules apply to the quotes; bash rules apply to the $ sigils. Since bash never looks at the quotes, it doesn't matter to bash what type of quotes they are; the $foo gets replaced regardless (unless you use \$; bash looks at backslashes).

Python规则适用于引号;bash规则适用于$ sigils。由于bash从不查看引号,因此抨击它们是什么类型的引号并不重要;$foo无论如何都会被替换(除非您使用\$;bash看着反斜杠)。

If you'd quoted the heredoc delimiter (<<<"END"), then bash would not attempt to replace variable expansions nor interpret backslashes.

如果您引用了heredoc delimiter (<<<"END"),那么bash将不会尝试替换变量扩展,也不会试图解释反斜杠。

The rules for heredocs are explained thoroughly in the bash manual.

本教程的规则在bash手册中有详细的说明。

Is it different from [python -c "…"]?

它与[python -c "…]不同吗?

Yes, bash interprets command-line arguments before passing them to the program, so bash rules apply and python only sees whatever quotes which were passed through.

是的,bash会在将命令行参数传递给程序之前对它们进行解释,因此,bash规则应用程序和python只会看到经过的任何引号。

The rules for words in the command line are explained thoroughly in the bash manual (read this section in context).

命令行中单词的规则在bash手册中得到了详细的解释(请在上下文中阅读本节)。