python - 将多个参数从 bash 传递到 python

标签 python string bash command-line-arguments

我有一个从 bash 脚本调用的简短内联 python 脚本,我想让它处理一个多词变量(来自 $*)。我希望这能正常工作:

#!/bin/bash

arg="A B C"
python -c "print '"$arg"'"

但它没有:

  File "<string>", line 1
    print 'A
           ^
SyntaxError: EOL while scanning string literal

为什么?

最佳答案

BASH 脚本错误。

#!/bin/bash

arg="A B C"
python -c "print '$arg'"

并输出

$ sh test.sh 
A B C

请注意,要连接两个字符串变量,您不需要将它们放在字符串常量之外

关于python - 将多个参数从 bash 传递到 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29750203/

相关文章:

linux - Grep 文件中的多个模式并为每个模式输出前 5 个匹配项

python - 类型错误 : 'Index' object is not callable and SyntaxError: invalid syntax

python - 使用 python 将 pandas 数据帧导入 MS Access 表

Javascript 子字符串不返回正确的字符串

linux - 我的 Bash 脚本运行不正常

linux - 使用shell脚本读取OpenWrt中的最后一行串口

python - 使用 pandas 在 python 中读取 csv 文件时出错

python - 如何从数据框中的字符串创建列?

c - 为什么这个C程序的输出为零?

c++ - 用c++解析一个文件并忽略一些字符