How do I use variables in single quoted strings?
I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it).
echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}
any help would be greatly appreciated
string bash shell echo
add a comment |
I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it).
echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}
any help would be greatly appreciated
string bash shell echo
See also stackoverflow.com/questions/10067266/…
– tripleee
Jul 15 '18 at 19:06
See Difference between single and double quotes in Bash as well.
– codeforester
Sep 13 '18 at 21:53
add a comment |
I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it).
echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}
any help would be greatly appreciated
string bash shell echo
I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it).
echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}
any help would be greatly appreciated
string bash shell echo
string bash shell echo
edited Jun 30 '18 at 23:16
that other guy
71.8k885123
71.8k885123
asked Jan 17 '14 at 17:44
Pectus Excavatum
1,14892652
1,14892652
See also stackoverflow.com/questions/10067266/…
– tripleee
Jul 15 '18 at 19:06
See Difference between single and double quotes in Bash as well.
– codeforester
Sep 13 '18 at 21:53
add a comment |
See also stackoverflow.com/questions/10067266/…
– tripleee
Jul 15 '18 at 19:06
See Difference between single and double quotes in Bash as well.
– codeforester
Sep 13 '18 at 21:53
See also stackoverflow.com/questions/10067266/…
– tripleee
Jul 15 '18 at 19:06
See also stackoverflow.com/questions/10067266/…
– tripleee
Jul 15 '18 at 19:06
See Difference between single and double quotes in Bash as well.
– codeforester
Sep 13 '18 at 21:53
See Difference between single and double quotes in Bash as well.
– codeforester
Sep 13 '18 at 21:53
add a comment |
7 Answers
7
active
oldest
votes
Variables are expanded in double quoted strings, but not in single quoted strings:
$ name=World
$ echo "Hello $name"
Hello World
$ echo 'Hello $name'
Hello $name
If you can simply switch quotes, do so.
If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:
$ echo 'single quoted. '"Double quoted. "'Single quoted again.'
single quoted. Double quoted. Single quoted again.
$ echo '"$name" has the value '"$name"
"$name" has the value World
Applied to your case:
echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"
2
Alternatively,echo "test text "here_is_some_test_text_$counter" "output""
... Escape the double quotes you don't want the shell to interpret.
– twalberg
Jan 17 '14 at 18:03
5
Don't forget that you have to quote"$FILE"
.
– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:15
@Aleks-DanielJakimenko-A. Is it necessary?
– Josh Detwiler
Jul 2 '18 at 20:24
1
@JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
– Aleks-Daniel Jakimenko-A.
Jul 3 '18 at 21:02
add a comment |
use printf:
printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> ${FILE}
6
Please quote"$FILE"
.
– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:14
add a comment |
Use a heredoc:
cat << EOF >> ${FILE}
test text "here_is_some_test_text_$counter" "output"
EOF
add a comment |
The most readable, functional way uses curly braces inside double quotes.
'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"
1
Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
– Jonas Berlin
Oct 14 '16 at 22:00
@JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
– mklement0
Oct 14 '16 at 22:06
1
Unfortunately I do not have the reputation to leave a comment.
– Paul Back
Oct 17 '16 at 17:55
add a comment |
with a subshell:
var='hello' echo 'blah_'`echo $var`' blah blah';
1
Does not work, echoesblah_`echo $var` blah blah
– Jonas Berlin
Oct 14 '16 at 21:57
You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
– R.Sicart
Oct 18 '16 at 14:40
Your new answer will compress any whitespace in$var
.. please see Ignacio Vazquez-Abrams' answer..
– Jonas Berlin
Oct 19 '16 at 19:04
That's a useless use ofecho
. You'll be fine with'blah_'"$var"' blah blah.'
But that's already in Ignacio's answer.
– tripleee
Jul 2 '18 at 20:17
add a comment |
You can do it this way:
$ counter=1 eval echo `echo 'test text
"here_is_some_test_text_$counter" "output"' |
sed -s 's/"/\\"/g'` > file
cat file
test text "here_is_some_test_text_1" "output"
Explanation:
Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.
It says execute the following string as command:
'echo test text "here_is_some_test_text_$counter" "output"'
Command again in one line:
counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file
add a comment |
Output a variable wrapped with single quotes:
printf "'"'Hello %s'"'" world
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
autoActivateHeartbeat: false,
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%2fstackoverflow.com%2fquestions%2f21192420%2fhow-do-i-use-variables-in-single-quoted-strings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
Variables are expanded in double quoted strings, but not in single quoted strings:
$ name=World
$ echo "Hello $name"
Hello World
$ echo 'Hello $name'
Hello $name
If you can simply switch quotes, do so.
If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:
$ echo 'single quoted. '"Double quoted. "'Single quoted again.'
single quoted. Double quoted. Single quoted again.
$ echo '"$name" has the value '"$name"
"$name" has the value World
Applied to your case:
echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"
2
Alternatively,echo "test text "here_is_some_test_text_$counter" "output""
... Escape the double quotes you don't want the shell to interpret.
– twalberg
Jan 17 '14 at 18:03
5
Don't forget that you have to quote"$FILE"
.
– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:15
@Aleks-DanielJakimenko-A. Is it necessary?
– Josh Detwiler
Jul 2 '18 at 20:24
1
@JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
– Aleks-Daniel Jakimenko-A.
Jul 3 '18 at 21:02
add a comment |
Variables are expanded in double quoted strings, but not in single quoted strings:
$ name=World
$ echo "Hello $name"
Hello World
$ echo 'Hello $name'
Hello $name
If you can simply switch quotes, do so.
If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:
$ echo 'single quoted. '"Double quoted. "'Single quoted again.'
single quoted. Double quoted. Single quoted again.
$ echo '"$name" has the value '"$name"
"$name" has the value World
Applied to your case:
echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"
2
Alternatively,echo "test text "here_is_some_test_text_$counter" "output""
... Escape the double quotes you don't want the shell to interpret.
– twalberg
Jan 17 '14 at 18:03
5
Don't forget that you have to quote"$FILE"
.
– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:15
@Aleks-DanielJakimenko-A. Is it necessary?
– Josh Detwiler
Jul 2 '18 at 20:24
1
@JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
– Aleks-Daniel Jakimenko-A.
Jul 3 '18 at 21:02
add a comment |
Variables are expanded in double quoted strings, but not in single quoted strings:
$ name=World
$ echo "Hello $name"
Hello World
$ echo 'Hello $name'
Hello $name
If you can simply switch quotes, do so.
If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:
$ echo 'single quoted. '"Double quoted. "'Single quoted again.'
single quoted. Double quoted. Single quoted again.
$ echo '"$name" has the value '"$name"
"$name" has the value World
Applied to your case:
echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"
Variables are expanded in double quoted strings, but not in single quoted strings:
$ name=World
$ echo "Hello $name"
Hello World
$ echo 'Hello $name'
Hello $name
If you can simply switch quotes, do so.
If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:
$ echo 'single quoted. '"Double quoted. "'Single quoted again.'
single quoted. Double quoted. Single quoted again.
$ echo '"$name" has the value '"$name"
"$name" has the value World
Applied to your case:
echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"
edited Jul 3 '18 at 21:02
Aleks-Daniel Jakimenko-A.
7,5652931
7,5652931
answered Jan 17 '14 at 17:45
Ignacio Vazquez-Abrams
577k10010561158
577k10010561158
2
Alternatively,echo "test text "here_is_some_test_text_$counter" "output""
... Escape the double quotes you don't want the shell to interpret.
– twalberg
Jan 17 '14 at 18:03
5
Don't forget that you have to quote"$FILE"
.
– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:15
@Aleks-DanielJakimenko-A. Is it necessary?
– Josh Detwiler
Jul 2 '18 at 20:24
1
@JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
– Aleks-Daniel Jakimenko-A.
Jul 3 '18 at 21:02
add a comment |
2
Alternatively,echo "test text "here_is_some_test_text_$counter" "output""
... Escape the double quotes you don't want the shell to interpret.
– twalberg
Jan 17 '14 at 18:03
5
Don't forget that you have to quote"$FILE"
.
– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:15
@Aleks-DanielJakimenko-A. Is it necessary?
– Josh Detwiler
Jul 2 '18 at 20:24
1
@JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
– Aleks-Daniel Jakimenko-A.
Jul 3 '18 at 21:02
2
2
Alternatively,
echo "test text "here_is_some_test_text_$counter" "output""
... Escape the double quotes you don't want the shell to interpret.– twalberg
Jan 17 '14 at 18:03
Alternatively,
echo "test text "here_is_some_test_text_$counter" "output""
... Escape the double quotes you don't want the shell to interpret.– twalberg
Jan 17 '14 at 18:03
5
5
Don't forget that you have to quote
"$FILE"
.– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:15
Don't forget that you have to quote
"$FILE"
.– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:15
@Aleks-DanielJakimenko-A. Is it necessary?
– Josh Detwiler
Jul 2 '18 at 20:24
@Aleks-DanielJakimenko-A. Is it necessary?
– Josh Detwiler
Jul 2 '18 at 20:24
1
1
@JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
– Aleks-Daniel Jakimenko-A.
Jul 3 '18 at 21:02
@JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
– Aleks-Daniel Jakimenko-A.
Jul 3 '18 at 21:02
add a comment |
use printf:
printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> ${FILE}
6
Please quote"$FILE"
.
– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:14
add a comment |
use printf:
printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> ${FILE}
6
Please quote"$FILE"
.
– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:14
add a comment |
use printf:
printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> ${FILE}
use printf:
printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> ${FILE}
answered Jan 17 '14 at 18:07
glenn jackman
166k26143234
166k26143234
6
Please quote"$FILE"
.
– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:14
add a comment |
6
Please quote"$FILE"
.
– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:14
6
6
Please quote
"$FILE"
.– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:14
Please quote
"$FILE"
.– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:14
add a comment |
Use a heredoc:
cat << EOF >> ${FILE}
test text "here_is_some_test_text_$counter" "output"
EOF
add a comment |
Use a heredoc:
cat << EOF >> ${FILE}
test text "here_is_some_test_text_$counter" "output"
EOF
add a comment |
Use a heredoc:
cat << EOF >> ${FILE}
test text "here_is_some_test_text_$counter" "output"
EOF
Use a heredoc:
cat << EOF >> ${FILE}
test text "here_is_some_test_text_$counter" "output"
EOF
answered Jan 17 '14 at 18:55
William Pursell
130k32206236
130k32206236
add a comment |
add a comment |
The most readable, functional way uses curly braces inside double quotes.
'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"
1
Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
– Jonas Berlin
Oct 14 '16 at 22:00
@JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
– mklement0
Oct 14 '16 at 22:06
1
Unfortunately I do not have the reputation to leave a comment.
– Paul Back
Oct 17 '16 at 17:55
add a comment |
The most readable, functional way uses curly braces inside double quotes.
'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"
1
Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
– Jonas Berlin
Oct 14 '16 at 22:00
@JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
– mklement0
Oct 14 '16 at 22:06
1
Unfortunately I do not have the reputation to leave a comment.
– Paul Back
Oct 17 '16 at 17:55
add a comment |
The most readable, functional way uses curly braces inside double quotes.
'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"
The most readable, functional way uses curly braces inside double quotes.
'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"
answered Oct 14 '16 at 21:54
Paul Back
753915
753915
1
Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
– Jonas Berlin
Oct 14 '16 at 22:00
@JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
– mklement0
Oct 14 '16 at 22:06
1
Unfortunately I do not have the reputation to leave a comment.
– Paul Back
Oct 17 '16 at 17:55
add a comment |
1
Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
– Jonas Berlin
Oct 14 '16 at 22:00
@JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
– mklement0
Oct 14 '16 at 22:06
1
Unfortunately I do not have the reputation to leave a comment.
– Paul Back
Oct 17 '16 at 17:55
1
1
Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
– Jonas Berlin
Oct 14 '16 at 22:00
Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
– Jonas Berlin
Oct 14 '16 at 22:00
@JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
– mklement0
Oct 14 '16 at 22:06
@JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
– mklement0
Oct 14 '16 at 22:06
1
1
Unfortunately I do not have the reputation to leave a comment.
– Paul Back
Oct 17 '16 at 17:55
Unfortunately I do not have the reputation to leave a comment.
– Paul Back
Oct 17 '16 at 17:55
add a comment |
with a subshell:
var='hello' echo 'blah_'`echo $var`' blah blah';
1
Does not work, echoesblah_`echo $var` blah blah
– Jonas Berlin
Oct 14 '16 at 21:57
You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
– R.Sicart
Oct 18 '16 at 14:40
Your new answer will compress any whitespace in$var
.. please see Ignacio Vazquez-Abrams' answer..
– Jonas Berlin
Oct 19 '16 at 19:04
That's a useless use ofecho
. You'll be fine with'blah_'"$var"' blah blah.'
But that's already in Ignacio's answer.
– tripleee
Jul 2 '18 at 20:17
add a comment |
with a subshell:
var='hello' echo 'blah_'`echo $var`' blah blah';
1
Does not work, echoesblah_`echo $var` blah blah
– Jonas Berlin
Oct 14 '16 at 21:57
You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
– R.Sicart
Oct 18 '16 at 14:40
Your new answer will compress any whitespace in$var
.. please see Ignacio Vazquez-Abrams' answer..
– Jonas Berlin
Oct 19 '16 at 19:04
That's a useless use ofecho
. You'll be fine with'blah_'"$var"' blah blah.'
But that's already in Ignacio's answer.
– tripleee
Jul 2 '18 at 20:17
add a comment |
with a subshell:
var='hello' echo 'blah_'`echo $var`' blah blah';
with a subshell:
var='hello' echo 'blah_'`echo $var`' blah blah';
edited Oct 18 '16 at 14:40
answered Jan 17 '14 at 18:32
R.Sicart
56029
56029
1
Does not work, echoesblah_`echo $var` blah blah
– Jonas Berlin
Oct 14 '16 at 21:57
You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
– R.Sicart
Oct 18 '16 at 14:40
Your new answer will compress any whitespace in$var
.. please see Ignacio Vazquez-Abrams' answer..
– Jonas Berlin
Oct 19 '16 at 19:04
That's a useless use ofecho
. You'll be fine with'blah_'"$var"' blah blah.'
But that's already in Ignacio's answer.
– tripleee
Jul 2 '18 at 20:17
add a comment |
1
Does not work, echoesblah_`echo $var` blah blah
– Jonas Berlin
Oct 14 '16 at 21:57
You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
– R.Sicart
Oct 18 '16 at 14:40
Your new answer will compress any whitespace in$var
.. please see Ignacio Vazquez-Abrams' answer..
– Jonas Berlin
Oct 19 '16 at 19:04
That's a useless use ofecho
. You'll be fine with'blah_'"$var"' blah blah.'
But that's already in Ignacio's answer.
– tripleee
Jul 2 '18 at 20:17
1
1
Does not work, echoes
blah_`echo $var` blah blah
– Jonas Berlin
Oct 14 '16 at 21:57
Does not work, echoes
blah_`echo $var` blah blah
– Jonas Berlin
Oct 14 '16 at 21:57
You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
– R.Sicart
Oct 18 '16 at 14:40
You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
– R.Sicart
Oct 18 '16 at 14:40
Your new answer will compress any whitespace in
$var
.. please see Ignacio Vazquez-Abrams' answer..– Jonas Berlin
Oct 19 '16 at 19:04
Your new answer will compress any whitespace in
$var
.. please see Ignacio Vazquez-Abrams' answer..– Jonas Berlin
Oct 19 '16 at 19:04
That's a useless use of
echo
. You'll be fine with 'blah_'"$var"' blah blah.'
But that's already in Ignacio's answer.– tripleee
Jul 2 '18 at 20:17
That's a useless use of
echo
. You'll be fine with 'blah_'"$var"' blah blah.'
But that's already in Ignacio's answer.– tripleee
Jul 2 '18 at 20:17
add a comment |
You can do it this way:
$ counter=1 eval echo `echo 'test text
"here_is_some_test_text_$counter" "output"' |
sed -s 's/"/\\"/g'` > file
cat file
test text "here_is_some_test_text_1" "output"
Explanation:
Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.
It says execute the following string as command:
'echo test text "here_is_some_test_text_$counter" "output"'
Command again in one line:
counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file
add a comment |
You can do it this way:
$ counter=1 eval echo `echo 'test text
"here_is_some_test_text_$counter" "output"' |
sed -s 's/"/\\"/g'` > file
cat file
test text "here_is_some_test_text_1" "output"
Explanation:
Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.
It says execute the following string as command:
'echo test text "here_is_some_test_text_$counter" "output"'
Command again in one line:
counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file
add a comment |
You can do it this way:
$ counter=1 eval echo `echo 'test text
"here_is_some_test_text_$counter" "output"' |
sed -s 's/"/\\"/g'` > file
cat file
test text "here_is_some_test_text_1" "output"
Explanation:
Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.
It says execute the following string as command:
'echo test text "here_is_some_test_text_$counter" "output"'
Command again in one line:
counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file
You can do it this way:
$ counter=1 eval echo `echo 'test text
"here_is_some_test_text_$counter" "output"' |
sed -s 's/"/\\"/g'` > file
cat file
test text "here_is_some_test_text_1" "output"
Explanation:
Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.
It says execute the following string as command:
'echo test text "here_is_some_test_text_$counter" "output"'
Command again in one line:
counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file
answered May 20 '17 at 15:17
Kulimak Joco
91
91
add a comment |
add a comment |
Output a variable wrapped with single quotes:
printf "'"'Hello %s'"'" world
add a comment |
Output a variable wrapped with single quotes:
printf "'"'Hello %s'"'" world
add a comment |
Output a variable wrapped with single quotes:
printf "'"'Hello %s'"'" world
Output a variable wrapped with single quotes:
printf "'"'Hello %s'"'" world
answered Jul 3 '17 at 9:07
Elior Malul
30315
30315
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f21192420%2fhow-do-i-use-variables-in-single-quoted-strings%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
See also stackoverflow.com/questions/10067266/…
– tripleee
Jul 15 '18 at 19:06
See Difference between single and double quotes in Bash as well.
– codeforester
Sep 13 '18 at 21:53