bash “echo 'hello'; ls”与“echo 'hello' && ls”之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3573742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:31:56  来源:igfitidea点击:

Difference between "echo 'hello'; ls" vs "echo 'hello' && ls"?

bash

提问by never_had_a_name

I wonder what the difference between

我想知道两者有什么区别

"echo 'hello'; ls"

and

"echo 'hello' && ls"

is? they both do the same thing

是?他们都做同样的事情

回答by Hendra Jaya

"echo 'hello' && ls" means : execute "ls" if "echo 'hello'" runs successfully. To understand what is "successful" in bash. Try this :

"echo 'hello' && ls" 表示:如果 "echo 'hello'" 运行成功,则执行 "ls"。了解什么是 bash 中的“成功”。尝试这个 :

bash> cd /
bash> echo $?

bash> cd /
bash> echo $?

if the previous command runs successfully, you should see 0

如果前面的命令成功运行,您应该看到 0

After that, try this :

之后,试试这个:

bash> asdfdf
bash> echo $?

bash> asdfdf
bash> echo $?

You should see a non-zero value between 1 and 255. This means previous command didn't run successfully

您应该会看到 1 到 255 之间的非零值。这意味着之前的命令没有成功运行

On the other hand, "echo 'hello'; ls" means execute "ls" whether "echo 'hello'" runs successfully or not.

另一方面,“echo 'hello'; ls”表示无论“echo 'hello'”运行成功与否,都执行“ls”。

回答by DarkDust

The &&is the logical AND operator. The idea in its use in command1 && command2is that command2 is only evaluated/run if command1 was successful. So here lswill only be run if the echocommand returned successful (which will always be the case here, but you never know ;-). You could also write this as:

&&是逻辑运算符。其使用的想法command1 && command2是仅在 command1 成功时才评估/运行 command2。所以这里ls只会在echo命令返回成功时运行(这里总是如此,但你永远不知道;-)。你也可以这样写:

if echo 'hello'
then
  ls
fi

The semicolon just delimits two commands. So you could also write echo 'hello' ; lsas:

分号只是分隔两个命令。所以你也可以写成echo 'hello' ; ls

echo 'hello'
ls

Thus lswill also be executed even when echofails.

因此ls即使echo失败也会被执行。

BTW, successful in this context means that the program was exited with something like exit(0)and thus returned 0 as a return code (the $?shell variable tells you the return status of the last executed command).

顺便说一句,在这种情况下成功意味着程序以类似的方式退出,exit(0)因此返回 0 作为返回码($?shell 变量告诉您​​上次执行的命令的返回状态)。

回答by telent

To supplement DarkDust's answer:

补充DarkDust 的回答

So here ls will only be run if the echo command returned successful (which will always be the case here, but you never know ;-).

所以这里 ls 只会在 echo 命令返回成功时运行(这里总是这样,但你永远不知道;-)。

Well, not always. For example, the standard output stream may be unwritable

嗯,并非总是如此。例如,标准输出流可能是不可写的

:; ( echo "foo" && touch /tmp/succeeded) >/dev/full
bash: echo: write error: No space left on device
:; ls -l /tmp/succeeded
ls: cannot access /tmp/succeeded: No such file or directory

回答by Lekensteyn

Here, they do the same.

在这里,他们做同样的事情。

But take an other example:

但是再举一个例子:

cp file target && ls
cp file target; ls

In the first case, lswould only be executed if cpsucceeds. In the second case, no matter if cpfails or succeeds, lswill always be executed.

在第一种情况下,ls只有cp成功才会执行。在第二种情况下,无论cp失败还是成功,ls都将始终执行。