bash - 如何在shell中操作&特殊字符
问题描述
我想在 shell 脚本中操作 URL。我需要用&
分隔符剪切URL,并获取相应的字符串。
我尝试过example="$(cut -d'&' -f2- <<< $1)"
,但是当我执行此代码并尝试执行时echo $example
,它想要执行$example
内容。
有人能帮我吗 ?
解决方案
您可能只需要引用变量。
问题脚本:
#!/bin/bash
example="$(cut -d'&' -f2- <<< $1)"
echo $example
如果你通过Shellcheck运行它,你会在输出中得到这个:
example="$(cut -d'&' -f2- <<< $1)"
^-- SC2086: Double quote to prevent globbing and word splitting.
echo $example
^-- SC2086: Double quote to prevent globbing and word splitting.
固定脚本:
#!/bin/bash
example="$(cut -d'&' -f2- <<< "$1")"
echo "$example"
推荐阅读
- machine-learning - 我可以在拟合 CatBoostRegressor 时对评估集中的观察结果加权吗?
- spring - 如何使用带有休眠模板的spring在同一个bean类上创建一对多和多对多关系
- sqlite - 使用 powershell 检索 Mozilla Firefox 浏览器历史记录
- css - 如何使用自定义字体更改 Bootstrap 表单输入的字体样式
- java - 如何在 Pattern.compile("\\R").splitAsStream(input) 中包含尾随空字符串?
- javascript - 工作箱服务人员在除 Chrome 之外的任何地方工作:未捕获(承诺)DOMException
- scala - 是否收到询问模式覆盖消息
- laravel-5 - 是否有任何包或东西可以与 laravel-5.0 一起使用,将视频从我的 laravel 视图上传到单个 youtube 频道
- angular - 如何防止 mat-select 在值选择时跳转到选定值?
- html - 如何更改悬停框中的图像