[英]Why ”false -a true“ and "true -a false" in shell return a different results?
IMO, -a
and &&
are all and
operation in shell, so I assume true -a false
and false -a true
would both return "false". IMO,
-a
和&&
都是 shell 中and
操作,所以我假设true -a false
和false -a true
都会返回“false”。 Well the fact is那么事实是
[root@master ~] true -a false
[root@master ~] echo $?
0
[root@master ~] false -a true
[root@master ~] echo $?
1
[root@master ~] a=[ true -a false ]
[root@master ~] echo $a
true
[root@master ~] a=[ false -a true ]
[root@master ~] echo $a
true
Why did this happen, and what should I do if I want to operate and
in Shell?为什么会发生这种情况,如果我想在壳牌中操作
and
我应该怎么做?
Let's the general syntax of a command in shell is:让我们在 shell 中的命令的一般语法是:
[var=val...] cmd [args...]
The var=val
are exporting environment variable var
with the value val
for the duration of command cmd
. var=val
在命令cmd
的持续时间内使用值val
导出环境变量var
。
Why did this happen
为什么会发生这种情况
Both commands true
and false
ignore arguments.命令
true
和false
都忽略参数。 true anything
exits with 0 exit status, false anything
exits with non-zero exit status. true anything
以 0 退出状态退出, false anything
以非零退出状态退出。 true -a false
executes command true
with two arguments, sting -a
and string false
. true -a false
使用两个参数执行命令true
, sting -a
和 string false
。 true
ignores arguments, exits with zero exit status. true
忽略参数,以零退出状态退出。 The same for false
.相同的
false
。
a=[
exports a variable a
with the value of string [
to the environment of the next command. a=[
将值为字符串[
的变量a
导出到下一个命令的环境。
$ a=[ env | grep '^a='
a=[
a=[ true -a false ]
set's environment variable a
to the value of string [
and then executes command true
with 3 arguments string -a
, the string false
and the string ]
. a=[ true -a false ]
将环境变量a
设置为字符串[
的值,然后使用 3 个参数 string -a
、字符串false
和字符串]
执行命令true
。
a=[ true -a false ]
| | | | \---- third argument
| | | \---------- second argument
| | \------------- first argument
| \------------------ command to execute
\---------------------- var=val, exports a variable
true
ignores the arguments and true
once again, exits with exit status 0
. true
忽略参数,再次为true
,退出状态0
。 The same is for false
, with non-zero exit status. false
也是如此,退出状态非零。
The value echo $a
that shows true
is the value that you previously have set in your shell.显示
true
的值echo $a
是您之前在 shell 中设置的值。 The a=[ true ...
only set's the variable a
for the duraction of the command, after that the variable has it's own value. a=[ true ...
仅在命令的持续时间内设置变量a
,之后变量具有自己的值。
$ a=anything_here
$ a=anything_here2 true anything_here3
$ echo $?
0 # true exits with 0 exit status
$ echo $a
anything_here # the variable `a` preserved its value set before the command
Why ”false -a true“ and "true -a false" in shell return a different results?
为什么shell中的“false -a true”和“true -a false”返回不同的结果?
Because commands false
and true
exit with different exit status.因为命令
false
和true
以不同的退出状态退出。
Note: the strings true
false
[
]
-a
have no special meaning for shell.注意:字符串
true
false
[
]
-a
对于 shell 没有特殊含义。 These are all strings.这些都是字符串。 The string
-a
and ]
have special meaning when passed as an argument for the command [
.当作为命令
[
的参数传递时,字符串-a
和]
具有特殊含义。 It's still the string -a
, but when the executable [
is executed, it parses the arguments and acts specially on the string -a
.它仍然是字符串
-a
,但是当可执行文件[
被执行时,它会解析参数并专门作用于字符串-a
。 There happen to be executables named true
and false
.恰好有名为
true
和false
的可执行文件。 The strings [
, ]
, true
, false
or -a
by itself have no significant for shell, these are all just strings.字符串
[
, ]
、 true
、 false
或-a
本身对 shell 没有意义,这些都只是字符串。
-a and && are all and operation in shell,
-a 和 && 都是和 shell 中的操作,
No, -a
is the string -a
in shell.不,
-a
是 shell 中的字符串-a
。 &&
is an "and" operation in shell. &&
是 shell 中的“与”操作。 -a
is the "and" operator for [
or test
commands . -a
是[
或test
commands的“与”运算符。 Note: because of problems with [
, prefer to use [ ... ] && [ ... ]
instead of [ ... -a ... ]
.注意:由于
[
的问题,更喜欢使用[ ... ] && [ ... ]
而不是[ ... -a ... ]
。
References: https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/test.html , https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Simple-Command-Expansion , https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/true.html , https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/false.html , https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_09_01 .参考: https ://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/test.html,https: //www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Simple-Command -扩展, https : //pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/true.html,https : //pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/false.html,https ://pubs .opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_09_01 。
Thanks for KamilCuk's answer.感谢 KamilCuk 的回答。 After reading that ,here's my shorter version.
读完之后,这是我的简短版本。
true -a false
and false -a true
is just a command who contains two arguments( -a [false/true]). true -a false
和false -a true
只是一个包含两个参数的命令(-a [false/true])。 Its exit code depend on the command itself which return 0 and 1 respectively.它的退出代码取决于分别返回 0 和 1 的命令本身。
a=[ true -a false
is the same but variable a
is being set to '['
before execute true -a false
. a=[ true -a false
相同,但变量a
在执行true -a false
之前被设置为'['
。
a
shall be unset if not set -a
or it's will be '['
如果未设置
a
则应取消set -a
否则它将是'['
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.