difference between echo ($x) and a=“($x)”; echo $a2019 Community Moderator ElectionWhen is double-quoting necessary?What's meaning the of a pair of parentheses after a string literal?For loop brackets - C like syntaxUnderstanding Bash's Read-a-File Command SubstitutionWhy is this simple bash script throwing an error on if/then/else?echo variable with content from command substitutionC Shell Script syntax error “unexpected end of of file”Bash: Syntax error - Unexpected near token 'else'Centos gawk command line usageblocking/non-blocking pipes/redirects inside command substitutionsyntax error near unexpected token `do
Why is participating in the European Parliamentary elections used as a threat?
Does Doodling or Improvising on the Piano Have Any Benefits?
Personal or impersonal in a technical resume
Is there a reason to prefer HFS+ over APFS for disk images in High Sierra and/or Mojave?
Identifying "long and narrow" polygons in with PostGIS
How to leave product feedback on macOS?
Has the laser at Magurele, Romania reached a tenth of the Sun's power?
Would this string work as string?
Would a primitive species be able to learn English from reading books alone?
Echo with obfuscation
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
Can I say "fingers" when referring to toes?
Sound waves in different octaves
What is this high flying aircraft over Pennsylvania?
El Dorado Word Puzzle II: Videogame Edition
What is the meaning of "You've never met a graph you didn't like?"
Grepping string, but include all non-blank lines following each grep match
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
"Oh no!" in Latin
Overlapping circles covering polygon
Why do Radio Buttons not fill the entire outer circle?
Why does a 97 / 92 key piano exist by Bösendorfer?
ContourPlot — How do I color by contour curvature?
How do I prevent inappropriate ads from appearing in my game?
difference between echo ($x) and a=“($x)”; echo $a
2019 Community Moderator ElectionWhen is double-quoting necessary?What's meaning the of a pair of parentheses after a string literal?For loop brackets - C like syntaxUnderstanding Bash's Read-a-File Command SubstitutionWhy is this simple bash script throwing an error on if/then/else?echo variable with content from command substitutionC Shell Script syntax error “unexpected end of of file”Bash: Syntax error - Unexpected near token 'else'Centos gawk command line usageblocking/non-blocking pipes/redirects inside command substitutionsyntax error near unexpected token `do
When I execute the following in terminal:
x=1
a=($x)
echo $a #output: 1
echo ($x) #output: -bash: syntax error near unexpected token `$x'
Why are above outputs different?
Also why are the following 2 outputs different:
$(echo foo) #output: -bash: foo: command not found
(echo foo) #output: foo
P.S.: I am new to shell scripting and trying to understand command substitution: $(command)
bash shell scripting command-substitution
New contributor
add a comment |
When I execute the following in terminal:
x=1
a=($x)
echo $a #output: 1
echo ($x) #output: -bash: syntax error near unexpected token `$x'
Why are above outputs different?
Also why are the following 2 outputs different:
$(echo foo) #output: -bash: foo: command not found
(echo foo) #output: foo
P.S.: I am new to shell scripting and trying to understand command substitution: $(command)
bash shell scripting command-substitution
New contributor
add a comment |
When I execute the following in terminal:
x=1
a=($x)
echo $a #output: 1
echo ($x) #output: -bash: syntax error near unexpected token `$x'
Why are above outputs different?
Also why are the following 2 outputs different:
$(echo foo) #output: -bash: foo: command not found
(echo foo) #output: foo
P.S.: I am new to shell scripting and trying to understand command substitution: $(command)
bash shell scripting command-substitution
New contributor
When I execute the following in terminal:
x=1
a=($x)
echo $a #output: 1
echo ($x) #output: -bash: syntax error near unexpected token `$x'
Why are above outputs different?
Also why are the following 2 outputs different:
$(echo foo) #output: -bash: foo: command not found
(echo foo) #output: foo
P.S.: I am new to shell scripting and trying to understand command substitution: $(command)
bash shell scripting command-substitution
bash shell scripting command-substitution
New contributor
New contributor
New contributor
asked 20 hours ago
SunnySunny
31
31
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You are mixing the three different usages of parentheses in bash
: array definition, command substitution, and command grouping. Command substitution and command grouping is standard syntax and all POSIX sh
-like shells supports it. The array definition syntax is supported in shells that additionally have arrays.
Array Definition
Arrays are assigned to using compound assignments of the form
name=(value1 value2 … )
Your a=($x)
creates an array with a single element, whose value is the value specified by $x
, i.e. 1
. When you echo an array by its name, in your case echo $a
, it only echoes the first element of the array. So that's what you see, a 1
is printed to stdout.
If you have multiple elements in an array you can use "$arrayname[*]" or "$arrayname[@]" to access them. The first combines all array elements into a single argument while the second put each element as an individual argument.
This syntax is specific to bash
and to other shells that allows for arrays in a similar way.
Command substitution (POSIX)
Command substitution allows the output of a command to replace the
command itself. Command substitution occurs when a command is enclosed
as follows:
$(command)
Your command $(echo foo)
performs shell substitution and is parsed to foo
. So it is the same as you have typed foo
. Since the command foo
does not exist, bash complains about it.
Command grouping (POSIX)
Bash provides two ways to group a list of commands to be executed as a
unit. When commands are grouped, redirections may be applied to the
entire command list. For example, the output of all the commands in
the list may be redirected to a single stream.
( list )
Placing a list of commands between parentheses causes a
subshell environment to be created (see Command Execution
Environment), and each of the commands in list to be executed in that
subshell. Since the list is executed in a subshell, variable
assignments do not remain in effect after the subshell completes.
Your command (echo foo)
executes echo foo
in a subshell, so foo
is echoed. This has nothing to do with the command substitution. As the manual says, variable assignments in subshell do not remain in effect after the subshell completes. This can come handy if you want to write simple one-liners. For example, instead of
for l in 1..10; do mycommand $l; done; unset l
You can write,
( for l in 1..10; do mycommand $l; done )
Another helpful usage of a subshell is like using
( cd folder; ./mycommand )
instead of
cd folder; ./mycommand; cd -
Your command echo ($x)
falls into none of these three categories, and bash
reports a syntax error.
Quote your variables properly
It is also worth mentioning that you should quote your variable expansions whenever applicable (for example, when a string should contain the value of a variable). See When is double-quoting necessary? for details.
For example, the following would not invoke the particular constructs described above:
x=1
a="($x)"
echo "$a"
echo "($x)"
Here, both calls to echo
would output (1)
.
Links to bash
manual pages
https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html
https://www.gnu.org/software/bash/manual/html_node/Arrays.html
2
Thank you. I've revised a bit but feel free to make edits on it. I am not that good at guiding new users.
– Weijun Zhou
19 hours ago
thanks for the detailed explanation Weijun
– Sunny
5 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
);
);
Sunny is a new contributor. Be nice, and check out our Code of Conduct.
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%2f507344%2fdifference-between-echo-x-and-a-x-echo-a%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
You are mixing the three different usages of parentheses in bash
: array definition, command substitution, and command grouping. Command substitution and command grouping is standard syntax and all POSIX sh
-like shells supports it. The array definition syntax is supported in shells that additionally have arrays.
Array Definition
Arrays are assigned to using compound assignments of the form
name=(value1 value2 … )
Your a=($x)
creates an array with a single element, whose value is the value specified by $x
, i.e. 1
. When you echo an array by its name, in your case echo $a
, it only echoes the first element of the array. So that's what you see, a 1
is printed to stdout.
If you have multiple elements in an array you can use "$arrayname[*]" or "$arrayname[@]" to access them. The first combines all array elements into a single argument while the second put each element as an individual argument.
This syntax is specific to bash
and to other shells that allows for arrays in a similar way.
Command substitution (POSIX)
Command substitution allows the output of a command to replace the
command itself. Command substitution occurs when a command is enclosed
as follows:
$(command)
Your command $(echo foo)
performs shell substitution and is parsed to foo
. So it is the same as you have typed foo
. Since the command foo
does not exist, bash complains about it.
Command grouping (POSIX)
Bash provides two ways to group a list of commands to be executed as a
unit. When commands are grouped, redirections may be applied to the
entire command list. For example, the output of all the commands in
the list may be redirected to a single stream.
( list )
Placing a list of commands between parentheses causes a
subshell environment to be created (see Command Execution
Environment), and each of the commands in list to be executed in that
subshell. Since the list is executed in a subshell, variable
assignments do not remain in effect after the subshell completes.
Your command (echo foo)
executes echo foo
in a subshell, so foo
is echoed. This has nothing to do with the command substitution. As the manual says, variable assignments in subshell do not remain in effect after the subshell completes. This can come handy if you want to write simple one-liners. For example, instead of
for l in 1..10; do mycommand $l; done; unset l
You can write,
( for l in 1..10; do mycommand $l; done )
Another helpful usage of a subshell is like using
( cd folder; ./mycommand )
instead of
cd folder; ./mycommand; cd -
Your command echo ($x)
falls into none of these three categories, and bash
reports a syntax error.
Quote your variables properly
It is also worth mentioning that you should quote your variable expansions whenever applicable (for example, when a string should contain the value of a variable). See When is double-quoting necessary? for details.
For example, the following would not invoke the particular constructs described above:
x=1
a="($x)"
echo "$a"
echo "($x)"
Here, both calls to echo
would output (1)
.
Links to bash
manual pages
https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html
https://www.gnu.org/software/bash/manual/html_node/Arrays.html
2
Thank you. I've revised a bit but feel free to make edits on it. I am not that good at guiding new users.
– Weijun Zhou
19 hours ago
thanks for the detailed explanation Weijun
– Sunny
5 hours ago
add a comment |
You are mixing the three different usages of parentheses in bash
: array definition, command substitution, and command grouping. Command substitution and command grouping is standard syntax and all POSIX sh
-like shells supports it. The array definition syntax is supported in shells that additionally have arrays.
Array Definition
Arrays are assigned to using compound assignments of the form
name=(value1 value2 … )
Your a=($x)
creates an array with a single element, whose value is the value specified by $x
, i.e. 1
. When you echo an array by its name, in your case echo $a
, it only echoes the first element of the array. So that's what you see, a 1
is printed to stdout.
If you have multiple elements in an array you can use "$arrayname[*]" or "$arrayname[@]" to access them. The first combines all array elements into a single argument while the second put each element as an individual argument.
This syntax is specific to bash
and to other shells that allows for arrays in a similar way.
Command substitution (POSIX)
Command substitution allows the output of a command to replace the
command itself. Command substitution occurs when a command is enclosed
as follows:
$(command)
Your command $(echo foo)
performs shell substitution and is parsed to foo
. So it is the same as you have typed foo
. Since the command foo
does not exist, bash complains about it.
Command grouping (POSIX)
Bash provides two ways to group a list of commands to be executed as a
unit. When commands are grouped, redirections may be applied to the
entire command list. For example, the output of all the commands in
the list may be redirected to a single stream.
( list )
Placing a list of commands between parentheses causes a
subshell environment to be created (see Command Execution
Environment), and each of the commands in list to be executed in that
subshell. Since the list is executed in a subshell, variable
assignments do not remain in effect after the subshell completes.
Your command (echo foo)
executes echo foo
in a subshell, so foo
is echoed. This has nothing to do with the command substitution. As the manual says, variable assignments in subshell do not remain in effect after the subshell completes. This can come handy if you want to write simple one-liners. For example, instead of
for l in 1..10; do mycommand $l; done; unset l
You can write,
( for l in 1..10; do mycommand $l; done )
Another helpful usage of a subshell is like using
( cd folder; ./mycommand )
instead of
cd folder; ./mycommand; cd -
Your command echo ($x)
falls into none of these three categories, and bash
reports a syntax error.
Quote your variables properly
It is also worth mentioning that you should quote your variable expansions whenever applicable (for example, when a string should contain the value of a variable). See When is double-quoting necessary? for details.
For example, the following would not invoke the particular constructs described above:
x=1
a="($x)"
echo "$a"
echo "($x)"
Here, both calls to echo
would output (1)
.
Links to bash
manual pages
https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html
https://www.gnu.org/software/bash/manual/html_node/Arrays.html
2
Thank you. I've revised a bit but feel free to make edits on it. I am not that good at guiding new users.
– Weijun Zhou
19 hours ago
thanks for the detailed explanation Weijun
– Sunny
5 hours ago
add a comment |
You are mixing the three different usages of parentheses in bash
: array definition, command substitution, and command grouping. Command substitution and command grouping is standard syntax and all POSIX sh
-like shells supports it. The array definition syntax is supported in shells that additionally have arrays.
Array Definition
Arrays are assigned to using compound assignments of the form
name=(value1 value2 … )
Your a=($x)
creates an array with a single element, whose value is the value specified by $x
, i.e. 1
. When you echo an array by its name, in your case echo $a
, it only echoes the first element of the array. So that's what you see, a 1
is printed to stdout.
If you have multiple elements in an array you can use "$arrayname[*]" or "$arrayname[@]" to access them. The first combines all array elements into a single argument while the second put each element as an individual argument.
This syntax is specific to bash
and to other shells that allows for arrays in a similar way.
Command substitution (POSIX)
Command substitution allows the output of a command to replace the
command itself. Command substitution occurs when a command is enclosed
as follows:
$(command)
Your command $(echo foo)
performs shell substitution and is parsed to foo
. So it is the same as you have typed foo
. Since the command foo
does not exist, bash complains about it.
Command grouping (POSIX)
Bash provides two ways to group a list of commands to be executed as a
unit. When commands are grouped, redirections may be applied to the
entire command list. For example, the output of all the commands in
the list may be redirected to a single stream.
( list )
Placing a list of commands between parentheses causes a
subshell environment to be created (see Command Execution
Environment), and each of the commands in list to be executed in that
subshell. Since the list is executed in a subshell, variable
assignments do not remain in effect after the subshell completes.
Your command (echo foo)
executes echo foo
in a subshell, so foo
is echoed. This has nothing to do with the command substitution. As the manual says, variable assignments in subshell do not remain in effect after the subshell completes. This can come handy if you want to write simple one-liners. For example, instead of
for l in 1..10; do mycommand $l; done; unset l
You can write,
( for l in 1..10; do mycommand $l; done )
Another helpful usage of a subshell is like using
( cd folder; ./mycommand )
instead of
cd folder; ./mycommand; cd -
Your command echo ($x)
falls into none of these three categories, and bash
reports a syntax error.
Quote your variables properly
It is also worth mentioning that you should quote your variable expansions whenever applicable (for example, when a string should contain the value of a variable). See When is double-quoting necessary? for details.
For example, the following would not invoke the particular constructs described above:
x=1
a="($x)"
echo "$a"
echo "($x)"
Here, both calls to echo
would output (1)
.
Links to bash
manual pages
https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html
https://www.gnu.org/software/bash/manual/html_node/Arrays.html
You are mixing the three different usages of parentheses in bash
: array definition, command substitution, and command grouping. Command substitution and command grouping is standard syntax and all POSIX sh
-like shells supports it. The array definition syntax is supported in shells that additionally have arrays.
Array Definition
Arrays are assigned to using compound assignments of the form
name=(value1 value2 … )
Your a=($x)
creates an array with a single element, whose value is the value specified by $x
, i.e. 1
. When you echo an array by its name, in your case echo $a
, it only echoes the first element of the array. So that's what you see, a 1
is printed to stdout.
If you have multiple elements in an array you can use "$arrayname[*]" or "$arrayname[@]" to access them. The first combines all array elements into a single argument while the second put each element as an individual argument.
This syntax is specific to bash
and to other shells that allows for arrays in a similar way.
Command substitution (POSIX)
Command substitution allows the output of a command to replace the
command itself. Command substitution occurs when a command is enclosed
as follows:
$(command)
Your command $(echo foo)
performs shell substitution and is parsed to foo
. So it is the same as you have typed foo
. Since the command foo
does not exist, bash complains about it.
Command grouping (POSIX)
Bash provides two ways to group a list of commands to be executed as a
unit. When commands are grouped, redirections may be applied to the
entire command list. For example, the output of all the commands in
the list may be redirected to a single stream.
( list )
Placing a list of commands between parentheses causes a
subshell environment to be created (see Command Execution
Environment), and each of the commands in list to be executed in that
subshell. Since the list is executed in a subshell, variable
assignments do not remain in effect after the subshell completes.
Your command (echo foo)
executes echo foo
in a subshell, so foo
is echoed. This has nothing to do with the command substitution. As the manual says, variable assignments in subshell do not remain in effect after the subshell completes. This can come handy if you want to write simple one-liners. For example, instead of
for l in 1..10; do mycommand $l; done; unset l
You can write,
( for l in 1..10; do mycommand $l; done )
Another helpful usage of a subshell is like using
( cd folder; ./mycommand )
instead of
cd folder; ./mycommand; cd -
Your command echo ($x)
falls into none of these three categories, and bash
reports a syntax error.
Quote your variables properly
It is also worth mentioning that you should quote your variable expansions whenever applicable (for example, when a string should contain the value of a variable). See When is double-quoting necessary? for details.
For example, the following would not invoke the particular constructs described above:
x=1
a="($x)"
echo "$a"
echo "($x)"
Here, both calls to echo
would output (1)
.
Links to bash
manual pages
https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html
https://www.gnu.org/software/bash/manual/html_node/Arrays.html
edited 14 hours ago
answered 19 hours ago
Weijun ZhouWeijun Zhou
1,661427
1,661427
2
Thank you. I've revised a bit but feel free to make edits on it. I am not that good at guiding new users.
– Weijun Zhou
19 hours ago
thanks for the detailed explanation Weijun
– Sunny
5 hours ago
add a comment |
2
Thank you. I've revised a bit but feel free to make edits on it. I am not that good at guiding new users.
– Weijun Zhou
19 hours ago
thanks for the detailed explanation Weijun
– Sunny
5 hours ago
2
2
Thank you. I've revised a bit but feel free to make edits on it. I am not that good at guiding new users.
– Weijun Zhou
19 hours ago
Thank you. I've revised a bit but feel free to make edits on it. I am not that good at guiding new users.
– Weijun Zhou
19 hours ago
thanks for the detailed explanation Weijun
– Sunny
5 hours ago
thanks for the detailed explanation Weijun
– Sunny
5 hours ago
add a comment |
Sunny is a new contributor. Be nice, and check out our Code of Conduct.
Sunny is a new contributor. Be nice, and check out our Code of Conduct.
Sunny is a new contributor. Be nice, and check out our Code of Conduct.
Sunny is a new contributor. Be nice, and check out our Code of Conduct.
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%2f507344%2fdifference-between-echo-x-and-a-x-echo-a%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