In bash, why is `SHELL=/bin/sh lesspipe"` gramatically correct? [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This question already has an answer here:
What is this shell/Bash syntax: someVariable=someValue someCommand [duplicate]
4 answers
On Ubuntu-like linux, the default .bashrc
file contains a line:
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
Then why is SHELL=/bin/sh lesspipe
grammatically correct? What page of man bash
can I know the meaning of this statement? To me, this statement seems the same as, for example,
ls echo
, which is invalid. Also,
abc=hello echo $abc
makes no error but outputs null.
I couldn't find any relevant information in man bash
though I tried some /<expression>
search command on less
. Could anyone please tell me the interpretations of the command?
Note: I've read the post "Can you explain eval expression in the default .bashrc" but got nothing.
bash
marked as duplicate by steeldriver, Community⦠Aug 13 at 16:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
This question already has an answer here:
What is this shell/Bash syntax: someVariable=someValue someCommand [duplicate]
4 answers
On Ubuntu-like linux, the default .bashrc
file contains a line:
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
Then why is SHELL=/bin/sh lesspipe
grammatically correct? What page of man bash
can I know the meaning of this statement? To me, this statement seems the same as, for example,
ls echo
, which is invalid. Also,
abc=hello echo $abc
makes no error but outputs null.
I couldn't find any relevant information in man bash
though I tried some /<expression>
search command on less
. Could anyone please tell me the interpretations of the command?
Note: I've read the post "Can you explain eval expression in the default .bashrc" but got nothing.
bash
marked as duplicate by steeldriver, Community⦠Aug 13 at 16:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@steeldriver Thank you. Actually my post seems duplicate. But how did you find the post you cited? Before I post this entry, I surfed the internet, but couldn't find that. I would like to know not to repeat the same mistake.
â ynn
Aug 13 at 15:30
it's no biggie, I guess my search-fu is just stronger ;) The relevant section of the bash man page is probablySHELL GRAMMAR
-Simple Commands
where it says A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. but unless you're a syntax weenie that's rather hard to recognize IMHO
â steeldriver
Aug 13 at 15:34
@steeldriver Thank you. I should have first read through the chapter ofSHELL GRAMMAR
inman bash
.
â ynn
Aug 13 at 16:12
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
What is this shell/Bash syntax: someVariable=someValue someCommand [duplicate]
4 answers
On Ubuntu-like linux, the default .bashrc
file contains a line:
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
Then why is SHELL=/bin/sh lesspipe
grammatically correct? What page of man bash
can I know the meaning of this statement? To me, this statement seems the same as, for example,
ls echo
, which is invalid. Also,
abc=hello echo $abc
makes no error but outputs null.
I couldn't find any relevant information in man bash
though I tried some /<expression>
search command on less
. Could anyone please tell me the interpretations of the command?
Note: I've read the post "Can you explain eval expression in the default .bashrc" but got nothing.
bash
This question already has an answer here:
What is this shell/Bash syntax: someVariable=someValue someCommand [duplicate]
4 answers
On Ubuntu-like linux, the default .bashrc
file contains a line:
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
Then why is SHELL=/bin/sh lesspipe
grammatically correct? What page of man bash
can I know the meaning of this statement? To me, this statement seems the same as, for example,
ls echo
, which is invalid. Also,
abc=hello echo $abc
makes no error but outputs null.
I couldn't find any relevant information in man bash
though I tried some /<expression>
search command on less
. Could anyone please tell me the interpretations of the command?
Note: I've read the post "Can you explain eval expression in the default .bashrc" but got nothing.
This question already has an answer here:
What is this shell/Bash syntax: someVariable=someValue someCommand [duplicate]
4 answers
bash
bash
asked Aug 13 at 15:22
ynn
495
495
marked as duplicate by steeldriver, Community⦠Aug 13 at 16:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by steeldriver, Community⦠Aug 13 at 16:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@steeldriver Thank you. Actually my post seems duplicate. But how did you find the post you cited? Before I post this entry, I surfed the internet, but couldn't find that. I would like to know not to repeat the same mistake.
â ynn
Aug 13 at 15:30
it's no biggie, I guess my search-fu is just stronger ;) The relevant section of the bash man page is probablySHELL GRAMMAR
-Simple Commands
where it says A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. but unless you're a syntax weenie that's rather hard to recognize IMHO
â steeldriver
Aug 13 at 15:34
@steeldriver Thank you. I should have first read through the chapter ofSHELL GRAMMAR
inman bash
.
â ynn
Aug 13 at 16:12
add a comment |Â
@steeldriver Thank you. Actually my post seems duplicate. But how did you find the post you cited? Before I post this entry, I surfed the internet, but couldn't find that. I would like to know not to repeat the same mistake.
â ynn
Aug 13 at 15:30
it's no biggie, I guess my search-fu is just stronger ;) The relevant section of the bash man page is probablySHELL GRAMMAR
-Simple Commands
where it says A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. but unless you're a syntax weenie that's rather hard to recognize IMHO
â steeldriver
Aug 13 at 15:34
@steeldriver Thank you. I should have first read through the chapter ofSHELL GRAMMAR
inman bash
.
â ynn
Aug 13 at 16:12
@steeldriver Thank you. Actually my post seems duplicate. But how did you find the post you cited? Before I post this entry, I surfed the internet, but couldn't find that. I would like to know not to repeat the same mistake.
â ynn
Aug 13 at 15:30
@steeldriver Thank you. Actually my post seems duplicate. But how did you find the post you cited? Before I post this entry, I surfed the internet, but couldn't find that. I would like to know not to repeat the same mistake.
â ynn
Aug 13 at 15:30
it's no biggie, I guess my search-fu is just stronger ;) The relevant section of the bash man page is probably
SHELL GRAMMAR
- Simple Commands
where it says A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. but unless you're a syntax weenie that's rather hard to recognize IMHOâ steeldriver
Aug 13 at 15:34
it's no biggie, I guess my search-fu is just stronger ;) The relevant section of the bash man page is probably
SHELL GRAMMAR
- Simple Commands
where it says A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. but unless you're a syntax weenie that's rather hard to recognize IMHOâ steeldriver
Aug 13 at 15:34
@steeldriver Thank you. I should have first read through the chapter of
SHELL GRAMMAR
in man bash
.â ynn
Aug 13 at 16:12
@steeldriver Thank you. I should have first read through the chapter of
SHELL GRAMMAR
in man bash
.â ynn
Aug 13 at 16:12
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
As steeldriver said, SHELL=/bin/sh lesspipe
is a simple command, which is defined as a sequence of:
- optional variable assignments (here
SHELL=/bin/sh
), followed by...
blank-separated words and redirections (herelesspipe
), terminated by...- a control operator, which includes a newline character.
Your first example:
ls echo
is actually a valid command: only the first word is treated as a command to execute, the rest are given to that command as parametersù.
In the case of a ls
command, if there is a sub-directory named echo
in the current directory, its contents will be listed; if there is a non-directory named echo
, its name will be displayedò. If neither is true, the command will return an error and display a No such file or directory
error message.
Your second example:
abc=hello echo $abc
is again a simple command with a variable assignment, but it also includes a variable expansion ($abc
). The EXPANSION chapter of man bash
defines the various kinds of expansion and their order of operations.
After the command line has been split into words, the shell checks for expansions:
- brace expansion: nothing applicable
- tilde expansion: nothing applicable
- parameter and variable expansion: here
$abc
is replaced by the current value of shell or environment variable namedabc
. But since this command line has not been executed yet, the variable assignment earlier on the line (abc=hello
) is not in effect yet. As a result,$abc
is replaced by whatever valueabc
had before, or an empty string if it was not set. The empty string is then removed completely since it was not quoted. - command substitution: nothing applicable
- arithmetic expansion: nothing applicable
- word splitting: the only candidate for this would have been the result of the
$abc
variable expansion, but there is nothing to split in a null string. - pathname expansion: nothing applicable
Once the expansions are done, the variable abc
in the environment of the to-be-started utility is set to value "hello" and the command echo
is executed. It outputs just a single blank line.
ù strictly speaking, ls
is also passed as argument, as its argv[0]
. ls
uses that argument to know how it was invoked, uses it as well in the error messages it outputs, but doesn't consider it as an option or file to act upon.
ò if it is a symbolic link, but its target can't be resolved, some ls
implementations will however output an error message as if the file didn't exist
Thank you for your detailed answer. Is the command executed in sub-shell? Some answers of the post of which my post was judged as duplicate say the "simple command" is the same as(export var=value command)
. However, the chapters ofSimple Commands
andSIMPLE COMMAND EXPANSION
don't refer to it.
â ynn
Aug 13 at 16:17
@ynn TheCOMMAND EXECUTION ENVIRONMENT
chapter told me it's true: When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environment
â ynn
Aug 13 at 16:21
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
As steeldriver said, SHELL=/bin/sh lesspipe
is a simple command, which is defined as a sequence of:
- optional variable assignments (here
SHELL=/bin/sh
), followed by...
blank-separated words and redirections (herelesspipe
), terminated by...- a control operator, which includes a newline character.
Your first example:
ls echo
is actually a valid command: only the first word is treated as a command to execute, the rest are given to that command as parametersù.
In the case of a ls
command, if there is a sub-directory named echo
in the current directory, its contents will be listed; if there is a non-directory named echo
, its name will be displayedò. If neither is true, the command will return an error and display a No such file or directory
error message.
Your second example:
abc=hello echo $abc
is again a simple command with a variable assignment, but it also includes a variable expansion ($abc
). The EXPANSION chapter of man bash
defines the various kinds of expansion and their order of operations.
After the command line has been split into words, the shell checks for expansions:
- brace expansion: nothing applicable
- tilde expansion: nothing applicable
- parameter and variable expansion: here
$abc
is replaced by the current value of shell or environment variable namedabc
. But since this command line has not been executed yet, the variable assignment earlier on the line (abc=hello
) is not in effect yet. As a result,$abc
is replaced by whatever valueabc
had before, or an empty string if it was not set. The empty string is then removed completely since it was not quoted. - command substitution: nothing applicable
- arithmetic expansion: nothing applicable
- word splitting: the only candidate for this would have been the result of the
$abc
variable expansion, but there is nothing to split in a null string. - pathname expansion: nothing applicable
Once the expansions are done, the variable abc
in the environment of the to-be-started utility is set to value "hello" and the command echo
is executed. It outputs just a single blank line.
ù strictly speaking, ls
is also passed as argument, as its argv[0]
. ls
uses that argument to know how it was invoked, uses it as well in the error messages it outputs, but doesn't consider it as an option or file to act upon.
ò if it is a symbolic link, but its target can't be resolved, some ls
implementations will however output an error message as if the file didn't exist
Thank you for your detailed answer. Is the command executed in sub-shell? Some answers of the post of which my post was judged as duplicate say the "simple command" is the same as(export var=value command)
. However, the chapters ofSimple Commands
andSIMPLE COMMAND EXPANSION
don't refer to it.
â ynn
Aug 13 at 16:17
@ynn TheCOMMAND EXECUTION ENVIRONMENT
chapter told me it's true: When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environment
â ynn
Aug 13 at 16:21
add a comment |Â
up vote
2
down vote
accepted
As steeldriver said, SHELL=/bin/sh lesspipe
is a simple command, which is defined as a sequence of:
- optional variable assignments (here
SHELL=/bin/sh
), followed by...
blank-separated words and redirections (herelesspipe
), terminated by...- a control operator, which includes a newline character.
Your first example:
ls echo
is actually a valid command: only the first word is treated as a command to execute, the rest are given to that command as parametersù.
In the case of a ls
command, if there is a sub-directory named echo
in the current directory, its contents will be listed; if there is a non-directory named echo
, its name will be displayedò. If neither is true, the command will return an error and display a No such file or directory
error message.
Your second example:
abc=hello echo $abc
is again a simple command with a variable assignment, but it also includes a variable expansion ($abc
). The EXPANSION chapter of man bash
defines the various kinds of expansion and their order of operations.
After the command line has been split into words, the shell checks for expansions:
- brace expansion: nothing applicable
- tilde expansion: nothing applicable
- parameter and variable expansion: here
$abc
is replaced by the current value of shell or environment variable namedabc
. But since this command line has not been executed yet, the variable assignment earlier on the line (abc=hello
) is not in effect yet. As a result,$abc
is replaced by whatever valueabc
had before, or an empty string if it was not set. The empty string is then removed completely since it was not quoted. - command substitution: nothing applicable
- arithmetic expansion: nothing applicable
- word splitting: the only candidate for this would have been the result of the
$abc
variable expansion, but there is nothing to split in a null string. - pathname expansion: nothing applicable
Once the expansions are done, the variable abc
in the environment of the to-be-started utility is set to value "hello" and the command echo
is executed. It outputs just a single blank line.
ù strictly speaking, ls
is also passed as argument, as its argv[0]
. ls
uses that argument to know how it was invoked, uses it as well in the error messages it outputs, but doesn't consider it as an option or file to act upon.
ò if it is a symbolic link, but its target can't be resolved, some ls
implementations will however output an error message as if the file didn't exist
Thank you for your detailed answer. Is the command executed in sub-shell? Some answers of the post of which my post was judged as duplicate say the "simple command" is the same as(export var=value command)
. However, the chapters ofSimple Commands
andSIMPLE COMMAND EXPANSION
don't refer to it.
â ynn
Aug 13 at 16:17
@ynn TheCOMMAND EXECUTION ENVIRONMENT
chapter told me it's true: When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environment
â ynn
Aug 13 at 16:21
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
As steeldriver said, SHELL=/bin/sh lesspipe
is a simple command, which is defined as a sequence of:
- optional variable assignments (here
SHELL=/bin/sh
), followed by...
blank-separated words and redirections (herelesspipe
), terminated by...- a control operator, which includes a newline character.
Your first example:
ls echo
is actually a valid command: only the first word is treated as a command to execute, the rest are given to that command as parametersù.
In the case of a ls
command, if there is a sub-directory named echo
in the current directory, its contents will be listed; if there is a non-directory named echo
, its name will be displayedò. If neither is true, the command will return an error and display a No such file or directory
error message.
Your second example:
abc=hello echo $abc
is again a simple command with a variable assignment, but it also includes a variable expansion ($abc
). The EXPANSION chapter of man bash
defines the various kinds of expansion and their order of operations.
After the command line has been split into words, the shell checks for expansions:
- brace expansion: nothing applicable
- tilde expansion: nothing applicable
- parameter and variable expansion: here
$abc
is replaced by the current value of shell or environment variable namedabc
. But since this command line has not been executed yet, the variable assignment earlier on the line (abc=hello
) is not in effect yet. As a result,$abc
is replaced by whatever valueabc
had before, or an empty string if it was not set. The empty string is then removed completely since it was not quoted. - command substitution: nothing applicable
- arithmetic expansion: nothing applicable
- word splitting: the only candidate for this would have been the result of the
$abc
variable expansion, but there is nothing to split in a null string. - pathname expansion: nothing applicable
Once the expansions are done, the variable abc
in the environment of the to-be-started utility is set to value "hello" and the command echo
is executed. It outputs just a single blank line.
ù strictly speaking, ls
is also passed as argument, as its argv[0]
. ls
uses that argument to know how it was invoked, uses it as well in the error messages it outputs, but doesn't consider it as an option or file to act upon.
ò if it is a symbolic link, but its target can't be resolved, some ls
implementations will however output an error message as if the file didn't exist
As steeldriver said, SHELL=/bin/sh lesspipe
is a simple command, which is defined as a sequence of:
- optional variable assignments (here
SHELL=/bin/sh
), followed by...
blank-separated words and redirections (herelesspipe
), terminated by...- a control operator, which includes a newline character.
Your first example:
ls echo
is actually a valid command: only the first word is treated as a command to execute, the rest are given to that command as parametersù.
In the case of a ls
command, if there is a sub-directory named echo
in the current directory, its contents will be listed; if there is a non-directory named echo
, its name will be displayedò. If neither is true, the command will return an error and display a No such file or directory
error message.
Your second example:
abc=hello echo $abc
is again a simple command with a variable assignment, but it also includes a variable expansion ($abc
). The EXPANSION chapter of man bash
defines the various kinds of expansion and their order of operations.
After the command line has been split into words, the shell checks for expansions:
- brace expansion: nothing applicable
- tilde expansion: nothing applicable
- parameter and variable expansion: here
$abc
is replaced by the current value of shell or environment variable namedabc
. But since this command line has not been executed yet, the variable assignment earlier on the line (abc=hello
) is not in effect yet. As a result,$abc
is replaced by whatever valueabc
had before, or an empty string if it was not set. The empty string is then removed completely since it was not quoted. - command substitution: nothing applicable
- arithmetic expansion: nothing applicable
- word splitting: the only candidate for this would have been the result of the
$abc
variable expansion, but there is nothing to split in a null string. - pathname expansion: nothing applicable
Once the expansions are done, the variable abc
in the environment of the to-be-started utility is set to value "hello" and the command echo
is executed. It outputs just a single blank line.
ù strictly speaking, ls
is also passed as argument, as its argv[0]
. ls
uses that argument to know how it was invoked, uses it as well in the error messages it outputs, but doesn't consider it as an option or file to act upon.
ò if it is a symbolic link, but its target can't be resolved, some ls
implementations will however output an error message as if the file didn't exist
edited Aug 13 at 16:23
Stéphane Chazelas
284k53524862
284k53524862
answered Aug 13 at 16:02
telcoM
11.5k11333
11.5k11333
Thank you for your detailed answer. Is the command executed in sub-shell? Some answers of the post of which my post was judged as duplicate say the "simple command" is the same as(export var=value command)
. However, the chapters ofSimple Commands
andSIMPLE COMMAND EXPANSION
don't refer to it.
â ynn
Aug 13 at 16:17
@ynn TheCOMMAND EXECUTION ENVIRONMENT
chapter told me it's true: When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environment
â ynn
Aug 13 at 16:21
add a comment |Â
Thank you for your detailed answer. Is the command executed in sub-shell? Some answers of the post of which my post was judged as duplicate say the "simple command" is the same as(export var=value command)
. However, the chapters ofSimple Commands
andSIMPLE COMMAND EXPANSION
don't refer to it.
â ynn
Aug 13 at 16:17
@ynn TheCOMMAND EXECUTION ENVIRONMENT
chapter told me it's true: When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environment
â ynn
Aug 13 at 16:21
Thank you for your detailed answer. Is the command executed in sub-shell? Some answers of the post of which my post was judged as duplicate say the "simple command" is the same as
(export var=value command)
. However, the chapters of Simple Commands
and SIMPLE COMMAND EXPANSION
don't refer to it.â ynn
Aug 13 at 16:17
Thank you for your detailed answer. Is the command executed in sub-shell? Some answers of the post of which my post was judged as duplicate say the "simple command" is the same as
(export var=value command)
. However, the chapters of Simple Commands
and SIMPLE COMMAND EXPANSION
don't refer to it.â ynn
Aug 13 at 16:17
@ynn The
COMMAND EXECUTION ENVIRONMENT
chapter told me it's true: When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environmentâ ynn
Aug 13 at 16:21
@ynn The
COMMAND EXECUTION ENVIRONMENT
chapter told me it's true: When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environmentâ ynn
Aug 13 at 16:21
add a comment |Â
@steeldriver Thank you. Actually my post seems duplicate. But how did you find the post you cited? Before I post this entry, I surfed the internet, but couldn't find that. I would like to know not to repeat the same mistake.
â ynn
Aug 13 at 15:30
it's no biggie, I guess my search-fu is just stronger ;) The relevant section of the bash man page is probably
SHELL GRAMMAR
-Simple Commands
where it says A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. but unless you're a syntax weenie that's rather hard to recognize IMHOâ steeldriver
Aug 13 at 15:34
@steeldriver Thank you. I should have first read through the chapter of
SHELL GRAMMAR
inman bash
.â ynn
Aug 13 at 16:12