How to use variables inside back-quotes

Clash Royale CLAN TAG#URR8PPP
I have a bash script that accepts a parameter "$input" and (among other things) directs it into a program and collects the output.
Currently my line is:
OUTPUT=`./main.exe < $input`
But I get the following error:
$input: ambiguous redirect
What is the correct way to do it?
bash scripting io-redirection variable
add a comment |
I have a bash script that accepts a parameter "$input" and (among other things) directs it into a program and collects the output.
Currently my line is:
OUTPUT=`./main.exe < $input`
But I get the following error:
$input: ambiguous redirect
What is the correct way to do it?
bash scripting io-redirection variable
add a comment |
I have a bash script that accepts a parameter "$input" and (among other things) directs it into a program and collects the output.
Currently my line is:
OUTPUT=`./main.exe < $input`
But I get the following error:
$input: ambiguous redirect
What is the correct way to do it?
bash scripting io-redirection variable
I have a bash script that accepts a parameter "$input" and (among other things) directs it into a program and collects the output.
Currently my line is:
OUTPUT=`./main.exe < $input`
But I get the following error:
$input: ambiguous redirect
What is the correct way to do it?
bash scripting io-redirection variable
bash scripting io-redirection variable
asked Feb 15 at 13:27
Erel Segal-HaleviErel Segal-Halevi
283128
283128
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your variable's value probably contains spaces.
You should double quote it:
output=$( ./main.exe <"$input" )
The bash shell requires that the variable, in this context, is quoted and will otherwise perform word-splitting and filename globbing on its value, while other shell may not require this.
Also, note that $input here is the pathname of a file that will be connected to the standard input stream of your program, not an argument to main.exe (I might have misunderstood your text, but never the less). If you want to use $input as a command line argument instead, your command would look like
output=$( ./main.exe "$input" )
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
Also:
- Have backticks (i.e. `cmd`) in *sh shells been deprecated?
- Are there naming conventions for variables in shell scripts?
Shouldn't you add quotes around a $() block in the case the output of the command contains spaces? According to Why does my shell script choke on whitespace or other special characters? it should
– Ferrybig
Feb 15 at 16:13
@Ferrybig: It's not needed when used for variable assignment, the shell will not perform word splitting on it. See Shell Parameters. This is also mentioned in Gilles answer in the linked question When is double-quoting necessary?
– Jesse_b
Feb 15 at 16:55
2
@Ferrybig As Jesse said, but it would not hurt to always add them if you're uncertain about where and under what circumstances they are actually needed.
– Kusalananda
Feb 15 at 17:26
Actually the problem disappeared after I replaced the backticks with the $( ... ) syntax. Thanks!
– Erel Segal-Halevi
Feb 16 at 17:16
@ErelSegal-Halevi Just changing the backticks to$( ... )should not have made a difference. The issue is the quoting of the variable. Using an unquoted variable in< $inputshould still break horribly if the value in$inputcontains spaces.
– Kusalananda
Feb 16 at 17:31
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%2f500867%2fhow-to-use-variables-inside-back-quotes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your variable's value probably contains spaces.
You should double quote it:
output=$( ./main.exe <"$input" )
The bash shell requires that the variable, in this context, is quoted and will otherwise perform word-splitting and filename globbing on its value, while other shell may not require this.
Also, note that $input here is the pathname of a file that will be connected to the standard input stream of your program, not an argument to main.exe (I might have misunderstood your text, but never the less). If you want to use $input as a command line argument instead, your command would look like
output=$( ./main.exe "$input" )
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
Also:
- Have backticks (i.e. `cmd`) in *sh shells been deprecated?
- Are there naming conventions for variables in shell scripts?
Shouldn't you add quotes around a $() block in the case the output of the command contains spaces? According to Why does my shell script choke on whitespace or other special characters? it should
– Ferrybig
Feb 15 at 16:13
@Ferrybig: It's not needed when used for variable assignment, the shell will not perform word splitting on it. See Shell Parameters. This is also mentioned in Gilles answer in the linked question When is double-quoting necessary?
– Jesse_b
Feb 15 at 16:55
2
@Ferrybig As Jesse said, but it would not hurt to always add them if you're uncertain about where and under what circumstances they are actually needed.
– Kusalananda
Feb 15 at 17:26
Actually the problem disappeared after I replaced the backticks with the $( ... ) syntax. Thanks!
– Erel Segal-Halevi
Feb 16 at 17:16
@ErelSegal-Halevi Just changing the backticks to$( ... )should not have made a difference. The issue is the quoting of the variable. Using an unquoted variable in< $inputshould still break horribly if the value in$inputcontains spaces.
– Kusalananda
Feb 16 at 17:31
add a comment |
Your variable's value probably contains spaces.
You should double quote it:
output=$( ./main.exe <"$input" )
The bash shell requires that the variable, in this context, is quoted and will otherwise perform word-splitting and filename globbing on its value, while other shell may not require this.
Also, note that $input here is the pathname of a file that will be connected to the standard input stream of your program, not an argument to main.exe (I might have misunderstood your text, but never the less). If you want to use $input as a command line argument instead, your command would look like
output=$( ./main.exe "$input" )
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
Also:
- Have backticks (i.e. `cmd`) in *sh shells been deprecated?
- Are there naming conventions for variables in shell scripts?
Shouldn't you add quotes around a $() block in the case the output of the command contains spaces? According to Why does my shell script choke on whitespace or other special characters? it should
– Ferrybig
Feb 15 at 16:13
@Ferrybig: It's not needed when used for variable assignment, the shell will not perform word splitting on it. See Shell Parameters. This is also mentioned in Gilles answer in the linked question When is double-quoting necessary?
– Jesse_b
Feb 15 at 16:55
2
@Ferrybig As Jesse said, but it would not hurt to always add them if you're uncertain about where and under what circumstances they are actually needed.
– Kusalananda
Feb 15 at 17:26
Actually the problem disappeared after I replaced the backticks with the $( ... ) syntax. Thanks!
– Erel Segal-Halevi
Feb 16 at 17:16
@ErelSegal-Halevi Just changing the backticks to$( ... )should not have made a difference. The issue is the quoting of the variable. Using an unquoted variable in< $inputshould still break horribly if the value in$inputcontains spaces.
– Kusalananda
Feb 16 at 17:31
add a comment |
Your variable's value probably contains spaces.
You should double quote it:
output=$( ./main.exe <"$input" )
The bash shell requires that the variable, in this context, is quoted and will otherwise perform word-splitting and filename globbing on its value, while other shell may not require this.
Also, note that $input here is the pathname of a file that will be connected to the standard input stream of your program, not an argument to main.exe (I might have misunderstood your text, but never the less). If you want to use $input as a command line argument instead, your command would look like
output=$( ./main.exe "$input" )
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
Also:
- Have backticks (i.e. `cmd`) in *sh shells been deprecated?
- Are there naming conventions for variables in shell scripts?
Your variable's value probably contains spaces.
You should double quote it:
output=$( ./main.exe <"$input" )
The bash shell requires that the variable, in this context, is quoted and will otherwise perform word-splitting and filename globbing on its value, while other shell may not require this.
Also, note that $input here is the pathname of a file that will be connected to the standard input stream of your program, not an argument to main.exe (I might have misunderstood your text, but never the less). If you want to use $input as a command line argument instead, your command would look like
output=$( ./main.exe "$input" )
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
Also:
- Have backticks (i.e. `cmd`) in *sh shells been deprecated?
- Are there naming conventions for variables in shell scripts?
edited Feb 15 at 14:00
answered Feb 15 at 13:31
KusalanandaKusalananda
135k17255420
135k17255420
Shouldn't you add quotes around a $() block in the case the output of the command contains spaces? According to Why does my shell script choke on whitespace or other special characters? it should
– Ferrybig
Feb 15 at 16:13
@Ferrybig: It's not needed when used for variable assignment, the shell will not perform word splitting on it. See Shell Parameters. This is also mentioned in Gilles answer in the linked question When is double-quoting necessary?
– Jesse_b
Feb 15 at 16:55
2
@Ferrybig As Jesse said, but it would not hurt to always add them if you're uncertain about where and under what circumstances they are actually needed.
– Kusalananda
Feb 15 at 17:26
Actually the problem disappeared after I replaced the backticks with the $( ... ) syntax. Thanks!
– Erel Segal-Halevi
Feb 16 at 17:16
@ErelSegal-Halevi Just changing the backticks to$( ... )should not have made a difference. The issue is the quoting of the variable. Using an unquoted variable in< $inputshould still break horribly if the value in$inputcontains spaces.
– Kusalananda
Feb 16 at 17:31
add a comment |
Shouldn't you add quotes around a $() block in the case the output of the command contains spaces? According to Why does my shell script choke on whitespace or other special characters? it should
– Ferrybig
Feb 15 at 16:13
@Ferrybig: It's not needed when used for variable assignment, the shell will not perform word splitting on it. See Shell Parameters. This is also mentioned in Gilles answer in the linked question When is double-quoting necessary?
– Jesse_b
Feb 15 at 16:55
2
@Ferrybig As Jesse said, but it would not hurt to always add them if you're uncertain about where and under what circumstances they are actually needed.
– Kusalananda
Feb 15 at 17:26
Actually the problem disappeared after I replaced the backticks with the $( ... ) syntax. Thanks!
– Erel Segal-Halevi
Feb 16 at 17:16
@ErelSegal-Halevi Just changing the backticks to$( ... )should not have made a difference. The issue is the quoting of the variable. Using an unquoted variable in< $inputshould still break horribly if the value in$inputcontains spaces.
– Kusalananda
Feb 16 at 17:31
Shouldn't you add quotes around a $() block in the case the output of the command contains spaces? According to Why does my shell script choke on whitespace or other special characters? it should
– Ferrybig
Feb 15 at 16:13
Shouldn't you add quotes around a $() block in the case the output of the command contains spaces? According to Why does my shell script choke on whitespace or other special characters? it should
– Ferrybig
Feb 15 at 16:13
@Ferrybig: It's not needed when used for variable assignment, the shell will not perform word splitting on it. See Shell Parameters. This is also mentioned in Gilles answer in the linked question When is double-quoting necessary?
– Jesse_b
Feb 15 at 16:55
@Ferrybig: It's not needed when used for variable assignment, the shell will not perform word splitting on it. See Shell Parameters. This is also mentioned in Gilles answer in the linked question When is double-quoting necessary?
– Jesse_b
Feb 15 at 16:55
2
2
@Ferrybig As Jesse said, but it would not hurt to always add them if you're uncertain about where and under what circumstances they are actually needed.
– Kusalananda
Feb 15 at 17:26
@Ferrybig As Jesse said, but it would not hurt to always add them if you're uncertain about where and under what circumstances they are actually needed.
– Kusalananda
Feb 15 at 17:26
Actually the problem disappeared after I replaced the backticks with the $( ... ) syntax. Thanks!
– Erel Segal-Halevi
Feb 16 at 17:16
Actually the problem disappeared after I replaced the backticks with the $( ... ) syntax. Thanks!
– Erel Segal-Halevi
Feb 16 at 17:16
@ErelSegal-Halevi Just changing the backticks to
$( ... ) should not have made a difference. The issue is the quoting of the variable. Using an unquoted variable in < $input should still break horribly if the value in $input contains spaces.– Kusalananda
Feb 16 at 17:31
@ErelSegal-Halevi Just changing the backticks to
$( ... ) should not have made a difference. The issue is the quoting of the variable. Using an unquoted variable in < $input should still break horribly if the value in $input contains spaces.– Kusalananda
Feb 16 at 17:31
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%2f500867%2fhow-to-use-variables-inside-back-quotes%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