'&&'与“&"在Bash中使用'test'命令 [英] '&&' vs. '&' with the 'test' command in Bash

查看:95
本文介绍了'&&'与“&"在Bash中使用'test'命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

gndlp@ubuntu:~$ test -x examples.desktop  && echo $?
gndlp@ubuntu:~$ test -x examples.desktop  & echo $?
[1] 2992
0

在这种情况下,为什么Bash会表现出来?

Why is Bash acting the way it is in this situation?

测试命令是否只是没有完成,因此没有处理echo命令?

Is the test command simply not finishing and thus the echo command isn't processed?

推荐答案

&&&的含义本质上是不同的.

The meaning of && and & are intrinsically different.

  • Bash中的&&是什么??在Bash和许多其他编程语言中,&&的意思是"AND".并且在这样的命令执行上下文中,这意味着在这种情况下,应按顺序运行&&左侧和右侧的项目.
  • Bash中的&是什么??单个&意味着前面的命令(在&的最左端)应该只在后台运行.
  • What is && in Bash? In Bash—and many other programming languages—&& means "AND". And in command execution context like this, it means items to the left as well as right of && should be run in sequence in this case.
  • What is & in Bash? And a single & means that the preceding commands—to the immediate left of the &—should simply be run in the background.

因此,请看您的示例:

gndlp@ubuntu:~$ test -x examples.desktop  && echo $?
gndlp@ubuntu:~$ test -x examples.desktop  & echo $?
[1] 2992
0

第一个命令(实际上是结构化的)实际上不返回任何内容.但是第二条命令返回[1] 2992,其中2992引用在后台运行的进程ID(PID),而0是第一条命令的输出.

The first command—as it is structured—actually does not return anything. But second command returns a [1] 2992 in which the 2992 refers to the process ID (PID) that is running in the background and the 0 is the output of the first command.

由于第二个命令仅在后台运行test -x examples.desktop,因此它很快发生,因此生成了进程ID,并立即消失了.

Since the second command is just running test -x examples.desktop in the background it happens quite quickly, so the process ID is spawned and gone pretty immediately.

这篇关于'&&'与“&"在Bash中使用'test'命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆