我正在尝试在 shell 中使用 printf
格式化字符串,我将从文件中获取输入字符串,该字符串具有特殊字符,如 %,',"",,\user,\tan
等
如何转义输入字符串中的特殊字符?
例如
#!/bin/bash
#
string='';
function GET_LINES() {
string+="The path to K:\Users\ca, this is good";
string+="\n";
string+="The second line";
string+="\t";
string+="123"
string+="\n";
string+="It also has to be 100% nice than %99";
printf "$string";
}
GET_LINES;
我希望这会以我想要的格式打印
The path to K:\Users\ca, this is good
The second line 123
It also has to be 100% nice than %99
但它给出了意想不到的结果
./script: line 14: printf: missing unicode digit for \U
The path to K:\Users\ca, this is good
The second line 123
./script: line 14: printf: `%99': missing format character
It also has to be 100ice than
那么我怎样才能在打印时去掉特殊字符呢? echo -e
也有问题。
最佳答案
关于linux - 使用 printf 如何转义 shell 脚本中的特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25958430/