linux - Bash:-c:第 0 行:意外的 EOF
问题描述
我尝试在终端中运行以下命令:
bash -c 's="test"; s=$(echo "word1 word2 " | awk '{print $1;}'); echo $s;'
它给了我以下错误:
bash: -c: line 0: unexpected EOF while looking for matching `)'
bash: -c: line 1: syntax error: unexpected end of file
}); echo $s;: command not found
当我将脚本保存在文件中时,不会出现此问题。
解决方案
您使用 -c 传递给 bash 的参数使用单引号。这将“保护”内部双引号,但不能保护用于在awk
程序中隐藏 '$1' 的引号。
来自 bash Man
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.
鉴于只需要引用单个字符,请考虑在awk
程序中使用双引号,并转义 '$'
bash -c 's="test"; s=$(echo "word1 word2 " | awk "{print \$1;}"); echo $s;'
Output:
word1
推荐阅读
- php - PHP根据值连续遍历数组
- r - 每年每种类型有多少计数?使用 group_by() 或 Split()
- angular - 无法读取未定义的属性“名称”-Angular 6。所有字段都重复错误
- c# - c# Linq 获取实体查询中列表元素的id
- c# - 如何分离 NCrunch 节点服务使用的所有 LocalDb 数据库
- php - MSSQL 连接在第二次或下一次尝试时失败
- javascript - 我们在不同的页面上使用了三个不同的地图,它可以在 Chrome 中运行,但是呢?
- paypal-sandbox - 使用 JASON 动态设置 PayPal Checkout 交易
- c++ - 返回数组中值的指针地址
- magento2 - Magento 2 静态内容部署不编译更少的文件