命令逃脱bash中的字符串 [英] Command to escape a string in bash

查看:154
本文介绍了命令逃脱bash中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个bash命令,将一个字符串转换为东西是逃脱。这里有一个例子:

I need a bash command that will convert a string to something that is escaped. Here's an example:


echo "hello\world"|escape|someprog

在哪里逃生命令使你好\\世界变成了你好\\\\的世界。然后,它预计someprog可以用你好\\世界。当然,这是什么,我真的会做一个简单的例子。

Where the escape command makes "hello\world" into "hello\\world". Then, someprog can use "hello\world" as it expects. Of course, this is a simplified example of what I will really be doing.

推荐答案

在击:

printf "%q" "hello\world" | someprog

例如:

printf "%q" "hello\world"
hello\\world

这可以通过变量使用过:

This could be used through variables too:

printf -v var "%q\n" "hello\world"
echo "$var"
hello\\world

这篇关于命令逃脱bash中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆