Issue
Say I have a function that accepts a string and interpolates it inside quotes.
print_str() (
printf '"%s"\n' "$1"
)
Since the function encloses the input in double quotes, I want to escape all double quotes, if any, in the argument. But, critically, double quotes that are already escaped must be preceded by a backslash. In short, the function must add one layer of escaping to all double quotes in the argument, be that the first layer or an additional layer.
Examples:
'abc"def'
->'abc\"def'
'{ "json": "{ \"key\": \"value\" }" }'
->'{ \"json\": \"{ \\\"key\\\": \\\"value\\\" }\" }'
The tricky part for me is determining whether a backslash preceding a double quote is actually escaping the double quote, because, for example, in '\\"'
the backslash does not escape the double quote even though it immediately precedes the double quote.
I've thought about using sed
and checking whether the number of backslashes before a double quote is odd or even, but that seems pretty convoluted. I managed to get the solution I wanted by piping the input through jq -R .
, but I am wondering if there is another way to do it. In particular, I am looking for something that works in POSIX shell.
EDIT:
For further clarification, I am seeking some function that will add an additional layer of escaping to double quotes in a given string.
some_function() {
# IDK what the implementation would be
}
Then, some_function
in conjunction with print_str
would producing the following output.
string='{ "json": "{ \"key\": \"value\" }" }'
print_str "$(some_function "$string")"
# -> prints "{ \"json\": \"{ \\\"key\\\": \\\"value\\\" }\" }"
Solution
You don't need to count anything
sed 's/["\]/\\&/g; s/.*/"&"/'
Answered By - oguz ismail Answer Checked By - Katrina (WPSolving Volunteer)