The Computer Oracle

Is there a way to escape single-quotes in the shell?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay

--

Chapters
00:00 Is There A Way To Escape Single-Quotes In The Shell?
00:28 Answer 1 Score 5
01:10 Accepted Answer Score 10
01:54 Answer 3 Score 1
02:24 Answer 4 Score 1
02:45 Thank you

--

Full question
https://superuser.com/questions/114798/i...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash #shell #escapecharacters #csh

#avk47



ACCEPTED ANSWER

Score 10


Dennis points out the usual alternatives in his answer (single-in-double, double-in-single, single quoted strings concatenated with escaped single quotes; you already mentioned Perl's customizable quote operators), but maybe this is the direct answer you were looking for.

In Bourne-like shells (sh, ksh, ash, dash, bash, zsh, etc.) single quoted strings are completely literal. This makes it easy to include characters that the shell would otherwise treat specially without having to escape them, but it does mean that the single quote character itself can not be included in single quoted strings.

I refuse to even think about the csh-type shells since they are utterly broken in other regards (see Csh Programming Considered Harmful).




ANSWER 2

Score 5


For me, all your examples produce:

Hello, world!

And so does this:

perl -e 'print "Hello, world!", "\n";'

And this:

perl -e 'print '\''Hello, world!'\'', "\n";'

Can you clarify what it is you're trying to do?

In the shell, such as Bash, if you want to print quotation marks as part of the string, one type of quotes escapes the other.

$ echo '"Hello, world!"'
"Hello, world!"
$ echo "'Hello, world!'"
'Hello, world!'

Edit:

Here's another way:

$ perl -e 'print "\047Hello, world!\047", "\n";'
'Hello, world!'
$ echo -e "\047Hello, world! \047"
'Hello, world! '

The space after the exclamation point avoids a history expansion error. I could have done set +H instead.




ANSWER 3

Score 1


Not sure if my edit to the previous answer would show up:

Pasting the same as a new answer:

Alternate approach:

When i tried your solution i could still not get the ' printed.

But i took cue from your solution and tried the following:

$ perl -e '$foo = chr(39); print "${foo}Hello, world ${foo}", "\n";'
'Hello, world '

ASCII value of ' is 39.

I needed this to make an inline edit to bunch of files to add ' at a location of interest.




ANSWER 4

Score 1


Here document is the best way to not care about escaping:

$ cat <<EOF
my $var = "Hello";
print $text, ', world!', "\n";'
EOF

You may completely inibit all expantion by using quoted here document:

$ cat <<'EOF'
my $var = "Hello";
print $text, ', world!', "\n";'
EOF

If you want to pass it as argument instead of input, you can use xargs:

$ xargs -0 <<'EOF' perl -e
my $var = "Hello";
print $text, ', world!', "\n";'
EOF