- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 bash 4.4,如果我想将参数打印到我的脚本并转义所有可能的非文字字符,那么我可以使用:
printf '%q\n' "${@}"
如果我想引用参数,那么我可以使用:
printf '%s\n' "${@@Q}"
例如:
$ cat tst.sh
#!/usr/bin/env bash
printf 'Escaped: %q\n' "${@}"
echo '---'
printf 'Quoted: %s\n' "${@@Q}"
$ ./tst.sh 'foo bar' 'other .* args'
Escaped: foo\ bar
Escaped: other\ .\*\ args
---
Quoted: 'foo bar'
Quoted: 'other .* args'
为什么?为什么我们不简单地使用一些格式说明符,例如 %E
用于 Escaped
和 %Q
用于 Quoted
而不是%q
的格式说明符表示 Escaped
,然后是一个完全不同的、神秘的 "${*@Q}"
语法,在使用时与 %s
组合表示 Quoted
?
使用字母 q
而不是 E
来生成 Escapes 令人费解,但我认为这背后有一些历史原因,但我更感兴趣的是出于技术原因,为什么他们不能在第二种情况下使用简单的 %Q
或类似的 printf 修饰符来提高我对 shell 一般工作方式的理解,因为现在我只是不明白为什么当前语法是必需的。
最佳答案
为了编写使用这些工具的脚本,唯一可以信任的保证是文档中给出的保证,如下所示:
对于 printf '%q'
:
%q
- quote the argument in a way that can be reused as shell input
对于@Q
参数转换:
The expansion is a string that is the value of parameter quoted in a format that can be reused as input.
值得注意的是,不能保证其中一个会使用单引号、反斜杠或任何其他特定形式; 唯一保证是两者都将生成可作为输入重复使用的输出。
其他一切都是未记录的实现细节,因此很容易发生变化。
此外,一些证明这两种构造的行为在实践中可能与问题中给出的描述不同:
$ nl=$'\n'
$ printf '%q\n' "$nl" # in bash 5.0, emits $'\n', not a backslash followed by a newline
$ printf '%s\n' "${nl@Q}" # in bash 5.0, emits $'\n', not a newline in single-quotes
...这进一步证明了这两种构造都可以生成不可移植到基线 POSIX shell 的代码,因此“重用为 shell 输入”或“重用为输入”规范指的是评估由 bash 本身。
关于bash - bash printf 转义/引用功能背后的基本原理是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56103838/
本文全面深入地探讨了Docker容器通信技术,从基础概念、网络模型、核心组件到实战应用。详细介绍了不同网络模式及其实现,提供了容器通信的技术细节和实用案例,旨在为专业从业者提供深入的技术洞见和实
📒博客首页:崇尚学技术的科班人 🍣今天给大家带来的文章是《Dubbo快速上手 -- 带你了解Dubbo使用、原理》🍣 🍣希望各位小伙伴们能够耐心的读完这篇文章🍣 🙏博主也在学习阶段,如若发
一、写在前面 我们经常使用npm install ,但是你是否思考过它内部的原理是什么? 1、执行npm install 它背后帮助我们完成了什么操作? 2、我们会发现还有一个成为package-lo
Base64 Base64 是什么?是将字节流转换成可打印字符、将可打印字符转换为字节流的一种算法。Base64 使用 64 个可打印字符来表示转换后的数据。 准确的来说,Base64 不算
目录 协程定义 生成器和yield语义 Future类 IOLoop类 coroutine函数装饰器 总结 tornado中的
切片,这是一个在go语言中引入的新的理念。它有一些特征如下: 对数组抽象 数组长度不固定 可追加元素 切片容量可增大 容量大小成片增加 我们先把上面的理念整理在这
文章来源:https://sourl.cn/HpZHvy 引 言 本文主要论述的是“RPC 实现原理”,那么首先明确一个问题什么是 RPC 呢?RPC 是 Remote Procedure Call
源码地址(包含所有与springmvc相关的,静态文件路径设置,request请求入参接受,返回值处理converter设置等等): spring-framework/WebMvcConfigurat
请通过简单的java类向我展示一个依赖注入(inject)原理的小例子虽然我已经了解了spring,但是如果我需要用简单的java类术语来解释它,那么你能通过一个简单的例子向我展示一下吗?提前致谢。
1、背景 我们平常使用手机和电脑上网,需要访问公网上的网络资源,如逛淘宝和刷视频,那么手机和电脑是怎么知道去哪里去拿到这个网络资源来下载到本地的呢? 就比如我去食堂拿吃的,我需要
大家好,我是飞哥! 现在 iptables 这个工具的应用似乎是越来越广了。不仅仅是在传统的防火墙、NAT 等功能出现,在今天流行的的 Docker、Kubernets、Istio 项目中也经
本篇涉及到的所有接口在公开文档中均无,需要下载 GitHub 上的源码,自己创建私有类的文档。 npm run generateDocumentation -- --private yarn gene
我最近在很多代码中注意到人们将硬编码的配置(如端口号等)值放在类/方法的深处,使其难以找到,也无法配置。 这是否违反了 SOLID 原则?如果不是,我是否可以向我的团队成员引用另一个“原则”来说明为什
我是 C#、WPF 和 MVVM 模式的新手。很抱歉这篇很长的帖子,我试图设定我所有的理解点(或不理解点)。 在研究了很多关于 WPF 提供的命令机制和 MVVM 模式的文本之后,我在弄清楚如何使用这
可比较的 jQuery 函数 $.post("/example/handler", {foo: 1, bar: 2}); 将创建一个带有 post 参数 foo=1&bar=2 的请求。鉴于 $htt
如果Django不使用“延迟查询执行”原则,主要问题是什么? q = Entry.objects.filter(headline__startswith="What") q = q.filter(
我今天发现.NET框架在做计算时遵循BODMAS操作顺序。即计算按以下顺序进行: 括号 订单 部门 乘法 添加 减法 但是我四处搜索并找不到任何文档确认 .NET 绝对 遵循此原则,是否有此类文档?如
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
API 回顾 在创建 Viewer 时可以直接指定 影像供给器(ImageryProvider),官方提供了一个非常简单的例子,即离屏例子(搜 offline): new Cesium.Viewer(
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我是一名优秀的程序员,十分优秀!