How can I do ANSI C quoting of an existing bash va

2019-07-01 11:27发布

I have looked at this question, but it does not cover my use case.

Suppose I have the variable foo which holds the four-character literal \x60.

I want to perform ANSI C Quoting on the contents of this variable and store it into another variable bar.

I tried the following, but none of them achieved the desired effect.

bar=$'$foo'   
echo $bar     
bar=$"$foo"     
echo $bar       

Output:

$foo
\x61

Desired output (actual value of \x61):

a

How might I achieve this in the general case, including non-printable characters? Note that in this case a was used just as an example to make it easier to test whether the method worked.

标签: bash sed quoting
6条回答
神经病院院长
2楼-- · 2019-07-01 11:49

The following works:

 eval bar=\$\'$x\'

The command bar=$'\x61' has to be constructed first, then eval evaluates the newly built command.

查看更多
欢心
3楼-- · 2019-07-01 11:59

Since bash 4.4 there is a variable expansion to do exactly that:

$ foo='\x61';     echo "$foo" "${foo@E}"
\x61 a

To set another variable use:

$ printf -v bar "${foo@E}";     echo "$bar"
a
查看更多
不美不萌又怎样
4楼-- · 2019-07-01 12:05

I just found out that I can do this. Edited based on comments.

bar=$( echo -ne "$foo" )
查看更多
劳资没心,怎么记你
5楼-- · 2019-07-01 12:06

Sample of conversion via shell. Problem, the code is octal using \0nnn and hexdecimal (not on all shell) using \xnn (where n are [hexa]digit)

foo="\65"
print "$( echo "$foo" | sed 's/\\/&0/' )"

5

with awk, you could certainly convert it directly

查看更多
forever°为你锁心
6楼-- · 2019-07-01 12:07

The best method I know is

  y=$(printf $(echo "$foo"|sed 's/%/%%/g'))

As mentioned in the comments, this trims trailing newlines from $foo. To overcome this:

moo=$(echo "${foo}:end"|sed 's/%/%%/g')
moo=$(printf "$moo")
moo=${moo%:end}
# the escaped string is in $moo
echo "+++${moo}---"
查看更多
Bombasti
7楼-- · 2019-07-01 12:09

By far the simplest solution, if you are using bash:

printf %b "$foo"

Or, to save it in another variable name bar:

printf -v bar %b "$foo"

From help printf:

In addition to the standard format specifications described in printf(1) and printf(3), printf interprets:

 %b        expand backslash escape sequences in the corresponding argument
 %q        quote the argument in a way that can be reused as shell input
 %(fmt)T output the date-time string resulting from using FMT as a format
         string for strftime(3)

There are edge cases, though:

\c terminates output, backslashes in \', \", and \? are not removed, and octal escapes beginning with \0 may contain up to four digits

查看更多
登录 后发表回答