Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While using printf how to escape special characters in shell script?

I am trying to format a string with printf in shell, i will get input string from a file , that have special characters like %,',"",,\user, \tan etc.

How to escape the special characters that are in the input string ?

Eg

#!/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;

i am expecting this will print in the format i want like

The path to K:\Users\ca, this is good
The second line   123
It also has to be 100% nice than %99

But its giving unexpected out puts

./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 

So how can i get rid of the special characters while printing.? echo -e also has the issue.

like image 933
Sarath Avatar asked Sep 21 '14 10:09

Sarath


1 Answers

Try

printf "%s\n" "$string"

See printf(1)

like image 109
Basile Starynkevitch Avatar answered Sep 23 '22 17:09

Basile Starynkevitch