混合 Bash-Python 片段中变量的双引号与单引号
Double vs single quotes for a variable in a mixed Bash-Python snippet
我完全无法将 Python 与 Bash 混合使用,这对我来说真的很难。
I know 在 Bash 中使用双引号还是单引号取决于您打算使用变量、转义符和特殊字符。
I also know, that using double vs single quotes in Python is matter of taste mostly and PEP8也证实了这一点。
但是如果我想像这样使用 Python 片段 inside Bash 怎么办?无论哪种方式都有效。
#!/bin/bash
arg3="something special"
python << END
import functions;
print functions.do_smth('$arg', '$arg2')
print len("$arg3");
END
Python 或 Bash 规则是否适用于上述代码段中的引号?
和Python代码这样分配有区别吗?
python -c "import functions; print functions.do_smth('$arg', '$arg2'); print len("$arg3");"
当我使用第一个或第二个变体时,引用是否重要?我将变量传递给函数还是简单地使用它有关系吗?
python -c "print 'great program eh'"
python -c "import special; do_smth_special_with('great program eh')"
完全不知道所有这些用例之间的区别!
特别是当我应该将 JSON 输出作为变量(也有该死的引号和转义符!)从 Bash 传递到 Python 函数时我遇到的许多问题。
这里真的需要彻底的解释。
Does Python or Bash rules apply for the quotes in [an unquoted heredoc]?
Python规则适用于引号; bash 规则适用于 $
印记。由于 bash 从不查看引号,因此 bash 引号的类型无关紧要; $foo
无论如何都会被替换(除非你使用 $
;bash 查看反斜杠)。
如果您引用了 heredoc 定界符 (<<<"END"
),那么 bash 不会尝试替换变量扩展,也不会解释反斜杠。
heredocs 的规则在 bash manual.
中有详尽的解释
Is it different from [python -c "…"]?
是的,bash 在将命令行参数传递给程序之前对其进行解释,因此 bash 规则适用并且 python 仅查看传递的任何引号。
bash manual 中详细解释了命令行中的单词规则(在上下文中阅读本节)。
我完全无法将 Python 与 Bash 混合使用,这对我来说真的很难。
I know 在 Bash 中使用双引号还是单引号取决于您打算使用变量、转义符和特殊字符。
I also know, that using double vs single quotes in Python is matter of taste mostly and PEP8也证实了这一点。
但是如果我想像这样使用 Python 片段 inside Bash 怎么办?无论哪种方式都有效。
#!/bin/bash
arg3="something special"
python << END
import functions;
print functions.do_smth('$arg', '$arg2')
print len("$arg3");
END
Python 或 Bash 规则是否适用于上述代码段中的引号?
和Python代码这样分配有区别吗?
python -c "import functions; print functions.do_smth('$arg', '$arg2'); print len("$arg3");"
当我使用第一个或第二个变体时,引用是否重要?我将变量传递给函数还是简单地使用它有关系吗?
python -c "print 'great program eh'"
python -c "import special; do_smth_special_with('great program eh')"
完全不知道所有这些用例之间的区别!
特别是当我应该将 JSON 输出作为变量(也有该死的引号和转义符!)从 Bash 传递到 Python 函数时我遇到的许多问题。
这里真的需要彻底的解释。
Does Python or Bash rules apply for the quotes in [an unquoted heredoc]?
Python规则适用于引号; bash 规则适用于 $
印记。由于 bash 从不查看引号,因此 bash 引号的类型无关紧要; $foo
无论如何都会被替换(除非你使用 $
;bash 查看反斜杠)。
如果您引用了 heredoc 定界符 (<<<"END"
),那么 bash 不会尝试替换变量扩展,也不会解释反斜杠。
heredocs 的规则在 bash manual.
中有详尽的解释Is it different from [python -c "…"]?
是的,bash 在将命令行参数传递给程序之前对其进行解释,因此 bash 规则适用并且 python 仅查看传递的任何引号。
bash manual 中详细解释了命令行中的单词规则(在上下文中阅读本节)。