0

How can I quote with brackets in order to prevent Bash interpretation? I know it's possible for command substitution:

echo \"`echo "I will be in quote"`\"
echo \"$(echo "I will be in quote")\" # same meaning

Despite this, I tried using backslashes, but it's become unreadable as recursion grows.

echo "\"I'm in quote\""
echo %("I'm in quote") # % is what I look for

2 Answers 2

2

If you just want to display double quotes, you can use single quotes around the entire expression:

echo '"I am quoted."'

However, this becomes difficult to use if you want to echo single quotes as well.

To print a line or block of text that contains both single and double quotes, you can use a here document, which avoids escaping quotes entirely:

cat << EOT 
"I am quoted."
"I'm quoted as well."
EOT

You don't have to use EOT. Any other word will do.

0

I'm not quite sure what you really want. Here are a couple of alternatives:

var="\"I'm in quotes\""
echo "$var"
var="I'm in quotes"
echo "\"$var\""
printf '"%s"\n' "I'm in quotes"
function echo_quoted () { printf '"%s"\n' "$*"; }
echo_quoted "I'm in quotes"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .