K
K
kizijo2019-04-01 16:32:12
linux
kizijo, 2019-04-01 16:32:12

How to correctly escape single quotes in linux?

Good afternoon.
I want to execute a query:
grep -rnw '/home/user/' -e '$_GET['s']'
But I can't figure out how to escape single quotes in $_GET['s'].
I ask for hints. Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Shitskov, 2019-04-01
@Zarom

Seems standard
\'

V
vreitech, 2019-04-01
@fzfx

to insert a character 'inside a string, you need to split this string into three consecutive substrings (for string concatenation to work): put a closing single quote (thus forming the first string), then an escaped single quote (the second string), then an opening single quote (beginning third line).
i.e. thing'sturns into 'thing'\''s'.
grep -rnw '/home/user/' -e '$_GET['\''s'\'']'

J
jcmvbkbc, 2019-04-01
@jcmvbkbc

how to escape single quotes

There is no way to escape anything in a string defined in single quotes -- all characters are literals, and the single quote ends the string. In a double-quoted string, a single quote can be escaped with a backslash, or not escaped - it is not some kind of special character.
The original query can be written like this:
but in any case it looks problematic: $in a regular expression, this is the end of the string, square brackets are a character from the set ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question