How do you escape apostrophe in single quoted string in bash?
up vote
16
down vote
favorite
I don't understand how bash evaluates escaping of apostrophe characters in single quoted strings.
Here is an example:
$ echo ''''Hello World''''
'Hello World' # works
$ echo ''Hello World''
> # expects you to continue input
I've tried looking for explanations to this but couldn't get anything. What is bash doing here?
bash shell-script string escape-characters
add a comment |
up vote
16
down vote
favorite
I don't understand how bash evaluates escaping of apostrophe characters in single quoted strings.
Here is an example:
$ echo ''''Hello World''''
'Hello World' # works
$ echo ''Hello World''
> # expects you to continue input
I've tried looking for explanations to this but couldn't get anything. What is bash doing here?
bash shell-script string escape-characters
echo ''Hello World''
– math
Nov 13 '12 at 8:11
add a comment |
up vote
16
down vote
favorite
up vote
16
down vote
favorite
I don't understand how bash evaluates escaping of apostrophe characters in single quoted strings.
Here is an example:
$ echo ''''Hello World''''
'Hello World' # works
$ echo ''Hello World''
> # expects you to continue input
I've tried looking for explanations to this but couldn't get anything. What is bash doing here?
bash shell-script string escape-characters
I don't understand how bash evaluates escaping of apostrophe characters in single quoted strings.
Here is an example:
$ echo ''''Hello World''''
'Hello World' # works
$ echo ''Hello World''
> # expects you to continue input
I've tried looking for explanations to this but couldn't get anything. What is bash doing here?
bash shell-script string escape-characters
bash shell-script string escape-characters
edited Mar 16 '15 at 22:32
asked Oct 1 '12 at 10:04
Kibet
218128
218128
echo ''Hello World''
– math
Nov 13 '12 at 8:11
add a comment |
echo ''Hello World''
– math
Nov 13 '12 at 8:11
echo ''Hello World''
– math
Nov 13 '12 at 8:11
echo ''Hello World''
– math
Nov 13 '12 at 8:11
add a comment |
4 Answers
4
active
oldest
votes
up vote
16
down vote
accepted
In single quotes, no escaping is possible. There is no way how to include a single quote into single quotes. See Quoting in man bash.
1
You're right. The trick is in that line 'A single quote may not occur betweeen single quotes even when preceded by a backslash' So it probably splits it into different parts.
– Kibet
Oct 1 '12 at 10:31
@Colin As soon as a single quote is inside of two other single quotes (but backslashed), the quoted quote isn't a real quote anymore. It is just a char with no special pairing characteristics.
– zero2cx
Oct 1 '12 at 11:14
1
@zero2cx: Not true:echo '''
– choroba
Mar 31 '14 at 21:00
@zero2cx: I would say "outside" instead of "inside".
– choroba
Apr 1 '14 at 16:14
2
@choroba not "totally" true, in bash you can doecho $''hello world''
– bufh
Dec 17 '14 at 14:58
add a comment |
up vote
5
down vote
In addition to POSIX-supported single- and double-quoting, bash
supplies an additional type of quoting to allow a small class of escaped characters (including a single quote) in a quoted string:
$ echo $''Hello World''
'Hello World'
See the QUOTING section in the bash
man page, near the end of the section. (Search for "ANSI C".)
add a comment |
up vote
3
down vote
To explain what is happening with your escaped apostrophes, we'll examine your second example (also see single quotes, or strong quotes):
$ echo ''Hello World''
> # expects you to continue input
Here, you've left the quotation hanging, as you've stated. Now trim the end and change it to:
v v v
$ echo ''Hello World # Echo two strings: '' and 'Hello World'.
Hello World ^
The "Hello World" sub-string wasn't quoted here, but it behaved as if it was strong quoted. Using your example again, trim the end differently this time:
vv v (plain apostrophe)
$ echo ''Hello World' # Will echo: '' and 'Hello World''
Hello World' ^^ # Note that the trailing ' char is backslash escaped.
The "Hello World" sub-string again behaves as if it were strong quoted, with only the added apostrophe (escaped, so no longer a single quote) at the end.
When another single quote is added to the end (your original example) the string is left hanging and waiting for a close-quote.
add a comment |
up vote
3
down vote
Simple example of escaping quotes in shell:
$ echo 'abc'''abc'
abc'abc
$ echo "abc"""abc"
abc"abc
It's done by closing already opened one ('
), placing escaped one ('
) to print, then opening another one ('
).
Alternatively:
$ echo 'abc'"'"'abc'
abc'abc
$ echo "abc"'"'"abc"
abc"abc
It's done by finishing already opened one ('
), placing quote in another quote ("'"
), then opening another one ('
).
What you did (''Hello World''
), is:
- Opened 1st apostrophe:
'
. - Closed right after it
'
, so the string becomes:''
.
Hello World
is not quotes.- Placed standalone apostrophe (
'
) without opening it. - Last apostrophe (
'
) is opening string, but there is no closing one which is expected.
So the correct example would be:
$ echo 'Hello World'
'Hello World'
Related: How to escape single-quotes within single-quoted strings?
You are the man, thank you!
– Saim
Dec 5 at 9:47
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f481797%2fhow-do-you-escape-apostrophe-in-single-quoted-string-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
16
down vote
accepted
In single quotes, no escaping is possible. There is no way how to include a single quote into single quotes. See Quoting in man bash.
1
You're right. The trick is in that line 'A single quote may not occur betweeen single quotes even when preceded by a backslash' So it probably splits it into different parts.
– Kibet
Oct 1 '12 at 10:31
@Colin As soon as a single quote is inside of two other single quotes (but backslashed), the quoted quote isn't a real quote anymore. It is just a char with no special pairing characteristics.
– zero2cx
Oct 1 '12 at 11:14
1
@zero2cx: Not true:echo '''
– choroba
Mar 31 '14 at 21:00
@zero2cx: I would say "outside" instead of "inside".
– choroba
Apr 1 '14 at 16:14
2
@choroba not "totally" true, in bash you can doecho $''hello world''
– bufh
Dec 17 '14 at 14:58
add a comment |
up vote
16
down vote
accepted
In single quotes, no escaping is possible. There is no way how to include a single quote into single quotes. See Quoting in man bash.
1
You're right. The trick is in that line 'A single quote may not occur betweeen single quotes even when preceded by a backslash' So it probably splits it into different parts.
– Kibet
Oct 1 '12 at 10:31
@Colin As soon as a single quote is inside of two other single quotes (but backslashed), the quoted quote isn't a real quote anymore. It is just a char with no special pairing characteristics.
– zero2cx
Oct 1 '12 at 11:14
1
@zero2cx: Not true:echo '''
– choroba
Mar 31 '14 at 21:00
@zero2cx: I would say "outside" instead of "inside".
– choroba
Apr 1 '14 at 16:14
2
@choroba not "totally" true, in bash you can doecho $''hello world''
– bufh
Dec 17 '14 at 14:58
add a comment |
up vote
16
down vote
accepted
up vote
16
down vote
accepted
In single quotes, no escaping is possible. There is no way how to include a single quote into single quotes. See Quoting in man bash.
In single quotes, no escaping is possible. There is no way how to include a single quote into single quotes. See Quoting in man bash.
answered Oct 1 '12 at 10:11
choroba
12.9k13039
12.9k13039
1
You're right. The trick is in that line 'A single quote may not occur betweeen single quotes even when preceded by a backslash' So it probably splits it into different parts.
– Kibet
Oct 1 '12 at 10:31
@Colin As soon as a single quote is inside of two other single quotes (but backslashed), the quoted quote isn't a real quote anymore. It is just a char with no special pairing characteristics.
– zero2cx
Oct 1 '12 at 11:14
1
@zero2cx: Not true:echo '''
– choroba
Mar 31 '14 at 21:00
@zero2cx: I would say "outside" instead of "inside".
– choroba
Apr 1 '14 at 16:14
2
@choroba not "totally" true, in bash you can doecho $''hello world''
– bufh
Dec 17 '14 at 14:58
add a comment |
1
You're right. The trick is in that line 'A single quote may not occur betweeen single quotes even when preceded by a backslash' So it probably splits it into different parts.
– Kibet
Oct 1 '12 at 10:31
@Colin As soon as a single quote is inside of two other single quotes (but backslashed), the quoted quote isn't a real quote anymore. It is just a char with no special pairing characteristics.
– zero2cx
Oct 1 '12 at 11:14
1
@zero2cx: Not true:echo '''
– choroba
Mar 31 '14 at 21:00
@zero2cx: I would say "outside" instead of "inside".
– choroba
Apr 1 '14 at 16:14
2
@choroba not "totally" true, in bash you can doecho $''hello world''
– bufh
Dec 17 '14 at 14:58
1
1
You're right. The trick is in that line 'A single quote may not occur betweeen single quotes even when preceded by a backslash' So it probably splits it into different parts.
– Kibet
Oct 1 '12 at 10:31
You're right. The trick is in that line 'A single quote may not occur betweeen single quotes even when preceded by a backslash' So it probably splits it into different parts.
– Kibet
Oct 1 '12 at 10:31
@Colin As soon as a single quote is inside of two other single quotes (but backslashed), the quoted quote isn't a real quote anymore. It is just a char with no special pairing characteristics.
– zero2cx
Oct 1 '12 at 11:14
@Colin As soon as a single quote is inside of two other single quotes (but backslashed), the quoted quote isn't a real quote anymore. It is just a char with no special pairing characteristics.
– zero2cx
Oct 1 '12 at 11:14
1
1
@zero2cx: Not true:
echo '''
– choroba
Mar 31 '14 at 21:00
@zero2cx: Not true:
echo '''
– choroba
Mar 31 '14 at 21:00
@zero2cx: I would say "outside" instead of "inside".
– choroba
Apr 1 '14 at 16:14
@zero2cx: I would say "outside" instead of "inside".
– choroba
Apr 1 '14 at 16:14
2
2
@choroba not "totally" true, in bash you can do
echo $''hello world''
– bufh
Dec 17 '14 at 14:58
@choroba not "totally" true, in bash you can do
echo $''hello world''
– bufh
Dec 17 '14 at 14:58
add a comment |
up vote
5
down vote
In addition to POSIX-supported single- and double-quoting, bash
supplies an additional type of quoting to allow a small class of escaped characters (including a single quote) in a quoted string:
$ echo $''Hello World''
'Hello World'
See the QUOTING section in the bash
man page, near the end of the section. (Search for "ANSI C".)
add a comment |
up vote
5
down vote
In addition to POSIX-supported single- and double-quoting, bash
supplies an additional type of quoting to allow a small class of escaped characters (including a single quote) in a quoted string:
$ echo $''Hello World''
'Hello World'
See the QUOTING section in the bash
man page, near the end of the section. (Search for "ANSI C".)
add a comment |
up vote
5
down vote
up vote
5
down vote
In addition to POSIX-supported single- and double-quoting, bash
supplies an additional type of quoting to allow a small class of escaped characters (including a single quote) in a quoted string:
$ echo $''Hello World''
'Hello World'
See the QUOTING section in the bash
man page, near the end of the section. (Search for "ANSI C".)
In addition to POSIX-supported single- and double-quoting, bash
supplies an additional type of quoting to allow a small class of escaped characters (including a single quote) in a quoted string:
$ echo $''Hello World''
'Hello World'
See the QUOTING section in the bash
man page, near the end of the section. (Search for "ANSI C".)
answered Oct 1 '12 at 14:50
chepner
4,7951324
4,7951324
add a comment |
add a comment |
up vote
3
down vote
To explain what is happening with your escaped apostrophes, we'll examine your second example (also see single quotes, or strong quotes):
$ echo ''Hello World''
> # expects you to continue input
Here, you've left the quotation hanging, as you've stated. Now trim the end and change it to:
v v v
$ echo ''Hello World # Echo two strings: '' and 'Hello World'.
Hello World ^
The "Hello World" sub-string wasn't quoted here, but it behaved as if it was strong quoted. Using your example again, trim the end differently this time:
vv v (plain apostrophe)
$ echo ''Hello World' # Will echo: '' and 'Hello World''
Hello World' ^^ # Note that the trailing ' char is backslash escaped.
The "Hello World" sub-string again behaves as if it were strong quoted, with only the added apostrophe (escaped, so no longer a single quote) at the end.
When another single quote is added to the end (your original example) the string is left hanging and waiting for a close-quote.
add a comment |
up vote
3
down vote
To explain what is happening with your escaped apostrophes, we'll examine your second example (also see single quotes, or strong quotes):
$ echo ''Hello World''
> # expects you to continue input
Here, you've left the quotation hanging, as you've stated. Now trim the end and change it to:
v v v
$ echo ''Hello World # Echo two strings: '' and 'Hello World'.
Hello World ^
The "Hello World" sub-string wasn't quoted here, but it behaved as if it was strong quoted. Using your example again, trim the end differently this time:
vv v (plain apostrophe)
$ echo ''Hello World' # Will echo: '' and 'Hello World''
Hello World' ^^ # Note that the trailing ' char is backslash escaped.
The "Hello World" sub-string again behaves as if it were strong quoted, with only the added apostrophe (escaped, so no longer a single quote) at the end.
When another single quote is added to the end (your original example) the string is left hanging and waiting for a close-quote.
add a comment |
up vote
3
down vote
up vote
3
down vote
To explain what is happening with your escaped apostrophes, we'll examine your second example (also see single quotes, or strong quotes):
$ echo ''Hello World''
> # expects you to continue input
Here, you've left the quotation hanging, as you've stated. Now trim the end and change it to:
v v v
$ echo ''Hello World # Echo two strings: '' and 'Hello World'.
Hello World ^
The "Hello World" sub-string wasn't quoted here, but it behaved as if it was strong quoted. Using your example again, trim the end differently this time:
vv v (plain apostrophe)
$ echo ''Hello World' # Will echo: '' and 'Hello World''
Hello World' ^^ # Note that the trailing ' char is backslash escaped.
The "Hello World" sub-string again behaves as if it were strong quoted, with only the added apostrophe (escaped, so no longer a single quote) at the end.
When another single quote is added to the end (your original example) the string is left hanging and waiting for a close-quote.
To explain what is happening with your escaped apostrophes, we'll examine your second example (also see single quotes, or strong quotes):
$ echo ''Hello World''
> # expects you to continue input
Here, you've left the quotation hanging, as you've stated. Now trim the end and change it to:
v v v
$ echo ''Hello World # Echo two strings: '' and 'Hello World'.
Hello World ^
The "Hello World" sub-string wasn't quoted here, but it behaved as if it was strong quoted. Using your example again, trim the end differently this time:
vv v (plain apostrophe)
$ echo ''Hello World' # Will echo: '' and 'Hello World''
Hello World' ^^ # Note that the trailing ' char is backslash escaped.
The "Hello World" sub-string again behaves as if it were strong quoted, with only the added apostrophe (escaped, so no longer a single quote) at the end.
When another single quote is added to the end (your original example) the string is left hanging and waiting for a close-quote.
edited Oct 2 '12 at 22:43
answered Oct 1 '12 at 10:35
zero2cx
603515
603515
add a comment |
add a comment |
up vote
3
down vote
Simple example of escaping quotes in shell:
$ echo 'abc'''abc'
abc'abc
$ echo "abc"""abc"
abc"abc
It's done by closing already opened one ('
), placing escaped one ('
) to print, then opening another one ('
).
Alternatively:
$ echo 'abc'"'"'abc'
abc'abc
$ echo "abc"'"'"abc"
abc"abc
It's done by finishing already opened one ('
), placing quote in another quote ("'"
), then opening another one ('
).
What you did (''Hello World''
), is:
- Opened 1st apostrophe:
'
. - Closed right after it
'
, so the string becomes:''
.
Hello World
is not quotes.- Placed standalone apostrophe (
'
) without opening it. - Last apostrophe (
'
) is opening string, but there is no closing one which is expected.
So the correct example would be:
$ echo 'Hello World'
'Hello World'
Related: How to escape single-quotes within single-quoted strings?
You are the man, thank you!
– Saim
Dec 5 at 9:47
add a comment |
up vote
3
down vote
Simple example of escaping quotes in shell:
$ echo 'abc'''abc'
abc'abc
$ echo "abc"""abc"
abc"abc
It's done by closing already opened one ('
), placing escaped one ('
) to print, then opening another one ('
).
Alternatively:
$ echo 'abc'"'"'abc'
abc'abc
$ echo "abc"'"'"abc"
abc"abc
It's done by finishing already opened one ('
), placing quote in another quote ("'"
), then opening another one ('
).
What you did (''Hello World''
), is:
- Opened 1st apostrophe:
'
. - Closed right after it
'
, so the string becomes:''
.
Hello World
is not quotes.- Placed standalone apostrophe (
'
) without opening it. - Last apostrophe (
'
) is opening string, but there is no closing one which is expected.
So the correct example would be:
$ echo 'Hello World'
'Hello World'
Related: How to escape single-quotes within single-quoted strings?
You are the man, thank you!
– Saim
Dec 5 at 9:47
add a comment |
up vote
3
down vote
up vote
3
down vote
Simple example of escaping quotes in shell:
$ echo 'abc'''abc'
abc'abc
$ echo "abc"""abc"
abc"abc
It's done by closing already opened one ('
), placing escaped one ('
) to print, then opening another one ('
).
Alternatively:
$ echo 'abc'"'"'abc'
abc'abc
$ echo "abc"'"'"abc"
abc"abc
It's done by finishing already opened one ('
), placing quote in another quote ("'"
), then opening another one ('
).
What you did (''Hello World''
), is:
- Opened 1st apostrophe:
'
. - Closed right after it
'
, so the string becomes:''
.
Hello World
is not quotes.- Placed standalone apostrophe (
'
) without opening it. - Last apostrophe (
'
) is opening string, but there is no closing one which is expected.
So the correct example would be:
$ echo 'Hello World'
'Hello World'
Related: How to escape single-quotes within single-quoted strings?
Simple example of escaping quotes in shell:
$ echo 'abc'''abc'
abc'abc
$ echo "abc"""abc"
abc"abc
It's done by closing already opened one ('
), placing escaped one ('
) to print, then opening another one ('
).
Alternatively:
$ echo 'abc'"'"'abc'
abc'abc
$ echo "abc"'"'"abc"
abc"abc
It's done by finishing already opened one ('
), placing quote in another quote ("'"
), then opening another one ('
).
What you did (''Hello World''
), is:
- Opened 1st apostrophe:
'
. - Closed right after it
'
, so the string becomes:''
.
Hello World
is not quotes.- Placed standalone apostrophe (
'
) without opening it. - Last apostrophe (
'
) is opening string, but there is no closing one which is expected.
So the correct example would be:
$ echo 'Hello World'
'Hello World'
Related: How to escape single-quotes within single-quoted strings?
edited Dec 5 at 10:44
answered Feb 28 '15 at 21:12
kenorb
10.6k1577110
10.6k1577110
You are the man, thank you!
– Saim
Dec 5 at 9:47
add a comment |
You are the man, thank you!
– Saim
Dec 5 at 9:47
You are the man, thank you!
– Saim
Dec 5 at 9:47
You are the man, thank you!
– Saim
Dec 5 at 9:47
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f481797%2fhow-do-you-escape-apostrophe-in-single-quoted-string-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
echo ''Hello World''
– math
Nov 13 '12 at 8:11