How to prevent command substitution on the command line?How to use a special character as a normal one?How can we prevent parameter expansion?Number of backslashes needed for escaping regex backslash on the command-lineHow do I echo an expression with both single and double quotes?Single quote within double quotes and the Bash reference manualWhat can single quote mean specially within double quotes?command substitution within single quotes for aliasWhat does POSIX require for quoted here documents inside command substitution?shell expanding rename command backreferencesBad substitution: no closing “`” in a heredoc / EOFCommand substitution, assignment, tests and spacesWhy does single quotes around arguments fail to prevent “syntax error near unexpected token '('”?
ECDSA: Why is SigningKey shorter than VerifyingKey
(3 of 11: Akari) What is Pyramid Cult's Favorite Car?
What are the cons of stateless password generators?
2 weeks and a tight budget to prepare for Z-day. How long can I hunker down?
Is it safe if the neutral lead is exposed and disconnected?
Why is the number of local variables used in a Java bytecode method not the most economical?
Anti-cheating: should there be a limit to a number of toilet breaks per game per player?
How can Paypal know my card is being used in another account?
Composing fill in the blanks
How to store my pliers and wire cutters on my desk?
Why does Canada require mandatory bilingualism in a lot of federal government posts?
Does dual boot harm a laptop battery or reduce its life?
How long until two planets become one?
Is this photo showing a woman standing in the nude before teenagers real?
Must a song using the A minor scale begin or end with an Am chord? If not, how can I tell what the scale is?
How did the Axis intend to hold the Caucasus?
Why use the "finally" keyword?
How can I say in Russian "I am not afraid to write anything"?
What is more environmentally friendly? An A320 or a car?
What is this 4 sharp symbol and what does it mean?
Exploiting the delay when a festival ticket is scanned
To find islands of 1 and 0 in matrix
Telling manager project isn't worth the effort?
If Trump gets impeached, how long would Pence be president?
How to prevent command substitution on the command line?
How to use a special character as a normal one?How can we prevent parameter expansion?Number of backslashes needed for escaping regex backslash on the command-lineHow do I echo an expression with both single and double quotes?Single quote within double quotes and the Bash reference manualWhat can single quote mean specially within double quotes?command substitution within single quotes for aliasWhat does POSIX require for quoted here documents inside command substitution?shell expanding rename command backreferencesBad substitution: no closing “`” in a heredoc / EOFCommand substitution, assignment, tests and spacesWhy does single quotes around arguments fail to prevent “syntax error near unexpected token '('”?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I find that when writing text as input to another program, any command substitutions in double quotes within the intended text are interpreted and expanded by the shell
The links in the answer here states that single quotes can be used to prevent parameter expansion or command substitution. However I'm finding that enclosing a command substitution in single-quotes also fails to stop the shell from expanding the command substitution
How do you prevent the shell from interpreting command substitutions that are intended as text rather than a command to be executed?
A demonstration
$ echo "`wc -l *`"
attempts to count lines in all files in the current directory
$ echo "'`wc -l *`'"
Same result, i.e. counts lines in all files in the current directory
update From this demonstration I've spotted that the problem seems to be that I am quoting the single quotes. I think enclosing single quotes and `
(backtick) in double quotes preserves the literal meaning of (i.e. suppresses) the single quotes but does not preserve the literal meaning of the backquote (i.e. backtick) that introduces the command substitution.
In my use case the input for another command needs to be quoted. With this document saying that:
A single-quote cannot occur within single quotes
How do you prevent a single-quoted command substitution from being expanded when the single-quoted command substitution is within a (double) quoted string? There should be a way to do it other than using backslash escapes
bash shell command-line quoting command-substitution
|
show 4 more comments
I find that when writing text as input to another program, any command substitutions in double quotes within the intended text are interpreted and expanded by the shell
The links in the answer here states that single quotes can be used to prevent parameter expansion or command substitution. However I'm finding that enclosing a command substitution in single-quotes also fails to stop the shell from expanding the command substitution
How do you prevent the shell from interpreting command substitutions that are intended as text rather than a command to be executed?
A demonstration
$ echo "`wc -l *`"
attempts to count lines in all files in the current directory
$ echo "'`wc -l *`'"
Same result, i.e. counts lines in all files in the current directory
update From this demonstration I've spotted that the problem seems to be that I am quoting the single quotes. I think enclosing single quotes and `
(backtick) in double quotes preserves the literal meaning of (i.e. suppresses) the single quotes but does not preserve the literal meaning of the backquote (i.e. backtick) that introduces the command substitution.
In my use case the input for another command needs to be quoted. With this document saying that:
A single-quote cannot occur within single quotes
How do you prevent a single-quoted command substitution from being expanded when the single-quoted command substitution is within a (double) quoted string? There should be a way to do it other than using backslash escapes
bash shell command-line quoting command-substitution
3
You meanecho '$(ls)'
does not produce$(ls)
as its output? Can you add the example that is not working for you in the question?
– NickD
8 hours ago
Note that adding single quotes within double quotes won't help.
– Andy Dalton
7 hours ago
@NickD good suggestion, I've updated the question
– MyWrathAcademia
7 hours ago
Can you not just use single quotes instead of double quotes?
– Andy Dalton
7 hours ago
1
You know, instead of us guessing, you should put your actual example in the question.
– NickD
4 hours ago
|
show 4 more comments
I find that when writing text as input to another program, any command substitutions in double quotes within the intended text are interpreted and expanded by the shell
The links in the answer here states that single quotes can be used to prevent parameter expansion or command substitution. However I'm finding that enclosing a command substitution in single-quotes also fails to stop the shell from expanding the command substitution
How do you prevent the shell from interpreting command substitutions that are intended as text rather than a command to be executed?
A demonstration
$ echo "`wc -l *`"
attempts to count lines in all files in the current directory
$ echo "'`wc -l *`'"
Same result, i.e. counts lines in all files in the current directory
update From this demonstration I've spotted that the problem seems to be that I am quoting the single quotes. I think enclosing single quotes and `
(backtick) in double quotes preserves the literal meaning of (i.e. suppresses) the single quotes but does not preserve the literal meaning of the backquote (i.e. backtick) that introduces the command substitution.
In my use case the input for another command needs to be quoted. With this document saying that:
A single-quote cannot occur within single quotes
How do you prevent a single-quoted command substitution from being expanded when the single-quoted command substitution is within a (double) quoted string? There should be a way to do it other than using backslash escapes
bash shell command-line quoting command-substitution
I find that when writing text as input to another program, any command substitutions in double quotes within the intended text are interpreted and expanded by the shell
The links in the answer here states that single quotes can be used to prevent parameter expansion or command substitution. However I'm finding that enclosing a command substitution in single-quotes also fails to stop the shell from expanding the command substitution
How do you prevent the shell from interpreting command substitutions that are intended as text rather than a command to be executed?
A demonstration
$ echo "`wc -l *`"
attempts to count lines in all files in the current directory
$ echo "'`wc -l *`'"
Same result, i.e. counts lines in all files in the current directory
update From this demonstration I've spotted that the problem seems to be that I am quoting the single quotes. I think enclosing single quotes and `
(backtick) in double quotes preserves the literal meaning of (i.e. suppresses) the single quotes but does not preserve the literal meaning of the backquote (i.e. backtick) that introduces the command substitution.
In my use case the input for another command needs to be quoted. With this document saying that:
A single-quote cannot occur within single quotes
How do you prevent a single-quoted command substitution from being expanded when the single-quoted command substitution is within a (double) quoted string? There should be a way to do it other than using backslash escapes
bash shell command-line quoting command-substitution
bash shell command-line quoting command-substitution
edited 7 hours ago
MyWrathAcademia
asked 8 hours ago
MyWrathAcademiaMyWrathAcademia
3462 silver badges12 bronze badges
3462 silver badges12 bronze badges
3
You meanecho '$(ls)'
does not produce$(ls)
as its output? Can you add the example that is not working for you in the question?
– NickD
8 hours ago
Note that adding single quotes within double quotes won't help.
– Andy Dalton
7 hours ago
@NickD good suggestion, I've updated the question
– MyWrathAcademia
7 hours ago
Can you not just use single quotes instead of double quotes?
– Andy Dalton
7 hours ago
1
You know, instead of us guessing, you should put your actual example in the question.
– NickD
4 hours ago
|
show 4 more comments
3
You meanecho '$(ls)'
does not produce$(ls)
as its output? Can you add the example that is not working for you in the question?
– NickD
8 hours ago
Note that adding single quotes within double quotes won't help.
– Andy Dalton
7 hours ago
@NickD good suggestion, I've updated the question
– MyWrathAcademia
7 hours ago
Can you not just use single quotes instead of double quotes?
– Andy Dalton
7 hours ago
1
You know, instead of us guessing, you should put your actual example in the question.
– NickD
4 hours ago
3
3
You mean
echo '$(ls)'
does not produce $(ls)
as its output? Can you add the example that is not working for you in the question?– NickD
8 hours ago
You mean
echo '$(ls)'
does not produce $(ls)
as its output? Can you add the example that is not working for you in the question?– NickD
8 hours ago
Note that adding single quotes within double quotes won't help.
– Andy Dalton
7 hours ago
Note that adding single quotes within double quotes won't help.
– Andy Dalton
7 hours ago
@NickD good suggestion, I've updated the question
– MyWrathAcademia
7 hours ago
@NickD good suggestion, I've updated the question
– MyWrathAcademia
7 hours ago
Can you not just use single quotes instead of double quotes?
– Andy Dalton
7 hours ago
Can you not just use single quotes instead of double quotes?
– Andy Dalton
7 hours ago
1
1
You know, instead of us guessing, you should put your actual example in the question.
– NickD
4 hours ago
You know, instead of us guessing, you should put your actual example in the question.
– NickD
4 hours ago
|
show 4 more comments
2 Answers
2
active
oldest
votes
Use single-quote strong quoting:
printf '%sn' '`wc -l *`'
And if you want to also include single quotes in that argument passed to printf
, you'd need to use different quotes for '
itself like:
printf '%sn' '`wc -l *` and a '"'"' character'
Or:
printf '%sn' '`wc -l *` and a ''' character'
Other alternatives include escaping the `
with backslash inside double quotes:
printf '%sn' "`wc -l *` and a ' character"
Or have `
be the result of some expansion:
backtick='`'
printf '%sn' "$backtickwc -l *$backtick and a ' character"
Also note that:
cat << 'EOF'
`wc -l *` and a ' character and a " character
EOF
to output arbitrary text without having to worry about quoting (note the quotes around the first EOF
).
In the fish
shell, you can embed '
within '...'
with '
:
printf '%sn' '`wc -l *` and a ' character'
but anyway `
is not special there, so:
printf '%sn' "`wc -l *` and a ' character"
would work as well.
In rc, es or zsh -o rcquotes
, you can insert a '
within '...'
with ''
:
printf '%sn' '`wc -l *` and a '' character'
See How to use a special character as a normal one? for more details.
add a comment |
You can escape the backticks by using a backslash as shown below:
echo "`wc -l *`"
New contributor
I mentioned prefering solving the problem without using backticks because you would need to escape every character you don't want to be interpreted by the shell where as if quoting could solve the problem you could use one single-quote to quote a substring that contains special characters such as$
, `` ` `` and ` ` that you don't want to be interpreted by the shell
– MyWrathAcademia
7 hours ago
ok, so the reason you need to use double quotes is because you have a variable that needs to be interpreted as well? So, as a result, echo 'wc -l *
' wont work as solution?
– user138278
6 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f532811%2fhow-to-prevent-command-substitution-on-the-command-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use single-quote strong quoting:
printf '%sn' '`wc -l *`'
And if you want to also include single quotes in that argument passed to printf
, you'd need to use different quotes for '
itself like:
printf '%sn' '`wc -l *` and a '"'"' character'
Or:
printf '%sn' '`wc -l *` and a ''' character'
Other alternatives include escaping the `
with backslash inside double quotes:
printf '%sn' "`wc -l *` and a ' character"
Or have `
be the result of some expansion:
backtick='`'
printf '%sn' "$backtickwc -l *$backtick and a ' character"
Also note that:
cat << 'EOF'
`wc -l *` and a ' character and a " character
EOF
to output arbitrary text without having to worry about quoting (note the quotes around the first EOF
).
In the fish
shell, you can embed '
within '...'
with '
:
printf '%sn' '`wc -l *` and a ' character'
but anyway `
is not special there, so:
printf '%sn' "`wc -l *` and a ' character"
would work as well.
In rc, es or zsh -o rcquotes
, you can insert a '
within '...'
with ''
:
printf '%sn' '`wc -l *` and a '' character'
See How to use a special character as a normal one? for more details.
add a comment |
Use single-quote strong quoting:
printf '%sn' '`wc -l *`'
And if you want to also include single quotes in that argument passed to printf
, you'd need to use different quotes for '
itself like:
printf '%sn' '`wc -l *` and a '"'"' character'
Or:
printf '%sn' '`wc -l *` and a ''' character'
Other alternatives include escaping the `
with backslash inside double quotes:
printf '%sn' "`wc -l *` and a ' character"
Or have `
be the result of some expansion:
backtick='`'
printf '%sn' "$backtickwc -l *$backtick and a ' character"
Also note that:
cat << 'EOF'
`wc -l *` and a ' character and a " character
EOF
to output arbitrary text without having to worry about quoting (note the quotes around the first EOF
).
In the fish
shell, you can embed '
within '...'
with '
:
printf '%sn' '`wc -l *` and a ' character'
but anyway `
is not special there, so:
printf '%sn' "`wc -l *` and a ' character"
would work as well.
In rc, es or zsh -o rcquotes
, you can insert a '
within '...'
with ''
:
printf '%sn' '`wc -l *` and a '' character'
See How to use a special character as a normal one? for more details.
add a comment |
Use single-quote strong quoting:
printf '%sn' '`wc -l *`'
And if you want to also include single quotes in that argument passed to printf
, you'd need to use different quotes for '
itself like:
printf '%sn' '`wc -l *` and a '"'"' character'
Or:
printf '%sn' '`wc -l *` and a ''' character'
Other alternatives include escaping the `
with backslash inside double quotes:
printf '%sn' "`wc -l *` and a ' character"
Or have `
be the result of some expansion:
backtick='`'
printf '%sn' "$backtickwc -l *$backtick and a ' character"
Also note that:
cat << 'EOF'
`wc -l *` and a ' character and a " character
EOF
to output arbitrary text without having to worry about quoting (note the quotes around the first EOF
).
In the fish
shell, you can embed '
within '...'
with '
:
printf '%sn' '`wc -l *` and a ' character'
but anyway `
is not special there, so:
printf '%sn' "`wc -l *` and a ' character"
would work as well.
In rc, es or zsh -o rcquotes
, you can insert a '
within '...'
with ''
:
printf '%sn' '`wc -l *` and a '' character'
See How to use a special character as a normal one? for more details.
Use single-quote strong quoting:
printf '%sn' '`wc -l *`'
And if you want to also include single quotes in that argument passed to printf
, you'd need to use different quotes for '
itself like:
printf '%sn' '`wc -l *` and a '"'"' character'
Or:
printf '%sn' '`wc -l *` and a ''' character'
Other alternatives include escaping the `
with backslash inside double quotes:
printf '%sn' "`wc -l *` and a ' character"
Or have `
be the result of some expansion:
backtick='`'
printf '%sn' "$backtickwc -l *$backtick and a ' character"
Also note that:
cat << 'EOF'
`wc -l *` and a ' character and a " character
EOF
to output arbitrary text without having to worry about quoting (note the quotes around the first EOF
).
In the fish
shell, you can embed '
within '...'
with '
:
printf '%sn' '`wc -l *` and a ' character'
but anyway `
is not special there, so:
printf '%sn' "`wc -l *` and a ' character"
would work as well.
In rc, es or zsh -o rcquotes
, you can insert a '
within '...'
with ''
:
printf '%sn' '`wc -l *` and a '' character'
See How to use a special character as a normal one? for more details.
answered 6 hours ago
Stéphane ChazelasStéphane Chazelas
327k57 gold badges636 silver badges1002 bronze badges
327k57 gold badges636 silver badges1002 bronze badges
add a comment |
add a comment |
You can escape the backticks by using a backslash as shown below:
echo "`wc -l *`"
New contributor
I mentioned prefering solving the problem without using backticks because you would need to escape every character you don't want to be interpreted by the shell where as if quoting could solve the problem you could use one single-quote to quote a substring that contains special characters such as$
, `` ` `` and ` ` that you don't want to be interpreted by the shell
– MyWrathAcademia
7 hours ago
ok, so the reason you need to use double quotes is because you have a variable that needs to be interpreted as well? So, as a result, echo 'wc -l *
' wont work as solution?
– user138278
6 hours ago
add a comment |
You can escape the backticks by using a backslash as shown below:
echo "`wc -l *`"
New contributor
I mentioned prefering solving the problem without using backticks because you would need to escape every character you don't want to be interpreted by the shell where as if quoting could solve the problem you could use one single-quote to quote a substring that contains special characters such as$
, `` ` `` and ` ` that you don't want to be interpreted by the shell
– MyWrathAcademia
7 hours ago
ok, so the reason you need to use double quotes is because you have a variable that needs to be interpreted as well? So, as a result, echo 'wc -l *
' wont work as solution?
– user138278
6 hours ago
add a comment |
You can escape the backticks by using a backslash as shown below:
echo "`wc -l *`"
New contributor
You can escape the backticks by using a backslash as shown below:
echo "`wc -l *`"
New contributor
New contributor
answered 7 hours ago
user138278user138278
313 bronze badges
313 bronze badges
New contributor
New contributor
I mentioned prefering solving the problem without using backticks because you would need to escape every character you don't want to be interpreted by the shell where as if quoting could solve the problem you could use one single-quote to quote a substring that contains special characters such as$
, `` ` `` and ` ` that you don't want to be interpreted by the shell
– MyWrathAcademia
7 hours ago
ok, so the reason you need to use double quotes is because you have a variable that needs to be interpreted as well? So, as a result, echo 'wc -l *
' wont work as solution?
– user138278
6 hours ago
add a comment |
I mentioned prefering solving the problem without using backticks because you would need to escape every character you don't want to be interpreted by the shell where as if quoting could solve the problem you could use one single-quote to quote a substring that contains special characters such as$
, `` ` `` and ` ` that you don't want to be interpreted by the shell
– MyWrathAcademia
7 hours ago
ok, so the reason you need to use double quotes is because you have a variable that needs to be interpreted as well? So, as a result, echo 'wc -l *
' wont work as solution?
– user138278
6 hours ago
I mentioned prefering solving the problem without using backticks because you would need to escape every character you don't want to be interpreted by the shell where as if quoting could solve the problem you could use one single-quote to quote a substring that contains special characters such as
$
, `` ` `` and ` ` that you don't want to be interpreted by the shell– MyWrathAcademia
7 hours ago
I mentioned prefering solving the problem without using backticks because you would need to escape every character you don't want to be interpreted by the shell where as if quoting could solve the problem you could use one single-quote to quote a substring that contains special characters such as
$
, `` ` `` and ` ` that you don't want to be interpreted by the shell– MyWrathAcademia
7 hours ago
ok, so the reason you need to use double quotes is because you have a variable that needs to be interpreted as well? So, as a result, echo '
wc -l *
' wont work as solution?– user138278
6 hours ago
ok, so the reason you need to use double quotes is because you have a variable that needs to be interpreted as well? So, as a result, echo '
wc -l *
' wont work as solution?– user138278
6 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f532811%2fhow-to-prevent-command-substitution-on-the-command-line%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
3
You mean
echo '$(ls)'
does not produce$(ls)
as its output? Can you add the example that is not working for you in the question?– NickD
8 hours ago
Note that adding single quotes within double quotes won't help.
– Andy Dalton
7 hours ago
@NickD good suggestion, I've updated the question
– MyWrathAcademia
7 hours ago
Can you not just use single quotes instead of double quotes?
– Andy Dalton
7 hours ago
1
You know, instead of us guessing, you should put your actual example in the question.
– NickD
4 hours ago