Home > OS >  POSIX Shell Escape Quotes in Dynamic String
POSIX Shell Escape Quotes in Dynamic String

Time:02-03

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.

CodePudding user response:

You don't need to count anything

sed 's/["\]/\\&/g; s/.*/"&"/'

CodePudding user response:

Typically, you change ' for '\'' and put ' on the beginning and end.

quotestr() { printf "'%s'" "$(printf "%s" "$1" | sed "s/'/'\\\\''/g")"; }
  • Related