1

I am writing a shell program to output another shell program to be evalled later. Is there some common shell program to print shell escaped for a string?

1
  • %q (bash-only) was pointed out below. But mind you, %q was broken for more than a decade and only recently fixed. See also stackoverflow.com/questions/15783701 Commented Oct 22, 2015 at 1:31

2 Answers 2

2

I'm not sure I understand you question. But the %q option of printf might be what you are looking for.

%q Output the corresponding argument in a format that can be reused as shell input

printf %q 'C:\ProgramFiles is a Windows path;'

outputs C:\\ProgramFiles\ is\ a\ Windows\ path\;

(In this example, simple quotes are needed – comment of Gordon Davisson – but this doesn't matter if you print from a variable or the output of a command.)

Sign up to request clarification or add additional context in comments.

Comments

0

You could use single quoted string as this is evaluated without any substitution.
For example the following commands are equivalent

cat abc\ hi.txt
cat 'abc hi.txt'

1 Comment

This wil fail if the string itself contains a single-quote.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.