[Bash]让人头晕的if条件

news/2025/10/22 5:21:44/文章来源:https://www.cnblogs.com/profesor/p/19156791

来自chatgpt

 

Ah, yes — Bash’s conditional syntax is notoriously confusing because it has several different “types” of tests, and each has subtle differences. Let’s break it down carefully. You were asking about if and the parentheses/brackets:


1. if <command>; then ... fi

The if in Bash tests the exit status of a command. Any command can be used. Example:

if ls /tmp; thenecho "Directory exists"
elseecho "Directory does not exist"
fi

Here, ls /tmp is a command. If it returns 0 (success), the then branch runs. This is the most general form.


2. [ ... ] — single brackets

This is the classic test command, inherited from /bin/test. You use it for strings, numbers, and file tests. Examples:

if [ "$a" -eq 5 ]; thenecho "a is 5"
fiif [ -f "/etc/passwd" ]; thenecho "File exists"
fi

Rules:

  • You must have spaces around [, ], and operators.

  • It’s POSIX-compatible.


3. [[ ... ]] — double brackets

This is Bash’s enhanced conditional. It supports more features:

  • Pattern matching with =~

  • No need to quote variables in many cases

  • Logical operators && and || inside

Example:

if [[ $a -eq 5 && $b -lt 10 ]]; thenecho "Both conditions true"
fiif [[ $string =~ ^[0-9]+$ ]]; thenecho "String is numeric"
fi

Note: [[ ... ]] is Bash-specific. Not POSIX.


4. (( ... )) — arithmetic evaluation

This is used only for arithmetic, integers only:

if (( a < 10 )); thenecho "a is less than 10"
fi

Inside (( )):

  • You don’t need $ to reference variables (optional)

  • Supports + - * / % and comparison operators <, <=, >, >=, ==, !=

  • Returns 0 (true) if the expression is nonzero


5. ( ... ) — subshell

This runs a command list in a subshell, not a test:

if ( cd /tmp && ls ); thenecho "cd and ls succeeded"
fi
  • Rarely used in if.

  • Mostly for grouping commands without affecting the current shell environment.


Summary Table

SyntaxPurposeNotes
command Any command’s exit status Most general form
[ ... ] Test strings, numbers, files POSIX-compatible, spaces required
[[ ... ]] Advanced test Bash-only, supports regex, logical ops
(( ... )) Arithmetic evaluation Bash-only, integers only
( ... ) Subshell execution Not a test; runs commands in a subshell

Key takeaway:

  • () is not a conditional, it’s a subshell.

  • [ ] is POSIX test.

  • [[ ]] is Bash’s better test.

  • (( )) is arithmetic test.

  • Plain if <command> is the catch-all.


If you want, I can draw a flow diagram showing what type of if accepts what — it’s incredibly clarifying.

Do you want me to do that?

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/942832.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

鸭子类型(Duck Typing)中的“类型”,指的是什么的类型?为什么很多人认为“Python 没有真正实现多态”?多态的核心目的是什么?鸭子类型如何实现多态?

鸭子类型(Duck Typing)中的“类型”,指的是什么的类型? 鸭子类型(Duck Typing)中的“类型”,指的是“具备特定行为的对象的类型”——它不是传统意义上“由类定义的类型”(如 int、str 或自定义类),而是“由…

tryhackme-预安全-windows基础-windows 基础知识1-16

tryhackme-Pre Security-Windows Fundamentals -Windows Fundamentals 1 房间地址:https://tryhackme.com/room/windowsfundamentals1xbx 这是网络安全入门的基础模块的计算机科学基础知识:Windows Fundamentals 1(…

YOLO11深度学习的遥感视角地面房屋建筑检测分割与分析系统 - MKT

YOLO11深度学习的遥感视角地面房屋建筑检测分割与分析系统 https://blog.csdn.net/qq_42589613/article/details/146162941一、软件核心功能介绍及效果演示软件主要功能1. 可进行遥感视角地面房屋建筑检测分割,分割一…

鸭子类型(Duck Typing)中的“类型”,指的是什么的类型?为什么很多人认为“Python 没有真正实现多态”

鸭子类型(Duck Typing)中的“类型”,指的是“具备特定行为的对象的类型”——它不是传统意义上“由类定义的类型”(如 int、str 或自定义类),而是“由对象具备的方法/属性(行为)所定义的逻辑类型”。简单说:“…

图像分割 Segment Anything(1-2)第二代 - MKT

图像分割 Segment Anything(1-2)第二代 大模型 8秒 1800*1200 压缩一半# 使用前需要先安装 SAM 2。代码需要python>=3.10、 以及torch>=2.5.1和。请按照此处的torchvision>=0.20.1说明安装 PyTorch 和 Tor…

对比c++中的多态和python的多态

C++ 和 Python 中的“多态”都围绕“同一接口、不同实现”的核心思想,但由于语言特性(静态类型 vs 动态类型)的差异,两者在实现方式、约束性、灵活性上有显著区别。以下从核心机制、实现条件、使用场景等维度对比:…

OAK-D-SR近红外相机 - MKT

OAK-D-SR近红外相机 https://www.oakchina.cn/2024/08/13/%E5%85%B7%E6%9C%89-sam2-%E5%88%86%E6%AE%B5%E7%9A%84-ndvi-%E6%97%A0%E4%BA%BA%E6%9C%BA/

结对项目-自动生成小学四则运算题目命令行程序

(一)这个作业属于哪个课程 https://edu.cnblogs.com/campus/gdgy/Class34Grade23ComputerScience这个作业要求在哪里 https://edu.cnblogs.com/campus/gdgy/Class34Grade23ComputerScience/homework/13479这个作业的…

tryhackme-预安全-linux 基础-Linux 基础知识(第二部分)-14

tryhackme-Pre Security-Linux Fundamentals-Linux Fundamentals Part 2 房间地址:https://tryhackme.com/room/linuxfundamentalspart2 这是网络安全入门的基础模块的计算机科学基础知识:Linux Fundamentals Part 2…

tryhackme-预安全-linux 基础-Linux 基础知识(第一部分)-13

tryhackme-Pre Security-Linux Fundamentals-Linux Fundamentals Part 1 房间地址:https://tryhackme.com/room/linuxfundamentalspart1 这是网络安全入门的基础模块的计算机科学基础知识:Linux Fundamentals Part 1…

我测试了七个主流后端框架的性能-结果让我重新思考了技术选型

说实话,在开始这次测试之前,我从来没想过性能差异会这么大。作为一个大三的计算机专业学生,我一直觉得框架选择主要看功能和生态,性能嘛,差不多就行了。直到上个月,我们实验室的一个项目因为并发量上来后服务器频…

tryhackme-预安全-网络如何工作-总结-12

tryhackme-Pre Security-How The Web Works-Putting it all together 房间地址:https://tryhackme.com/room/puttingitalltogether 这是网络安全入门的基础模块的计算机科学基础知识:Putting it all together(总结)…

目标检测 Grounding DINO 用语言指定要检测的目标 - MKT

目标检测 Grounding DINO 用语言指定要检测的目标https://github.com/IDEA-Research/GroundingDINO

图像分割 3D-Box-Segment-Anything(3)分割2D到3D点云分割 rgb相机 - MKT

图像分割 3D-Box-Segment-Anything(3)分割2D到3D点云分割 rgb相机https://github.com/dvlab-research/3D-Box-Segment-AnythingVoxelNeXt (CVPR 2023) [论文] [代码]用于 3D 对象检测和跟踪的完全稀疏 VoxelNet。

图像分割 Segment Anything(3)分割2D到3D点云分割 rgb-d相机 - MKT

图像分割 Segment Anything(3)分割2D到3D点云分割 rgb-d相机 https://github.com/Pointcept/SegmentAnything3D

Python 包管理工具推荐:uv

目录简介核心特性安装 uvLinux / macOS / WSL WindowsPython 版本管理安装和管理 Python 版本项目环境管理为新项目创建环境 为已有代码创建环境依赖管理添加依赖 从已有依赖文件迁移从 requirements.txt 导入 使用已有…

图像分割 Segment Anything(3)分割2D到3D点云分割 rgb相机 - MKT

图像分割 Segment Anything(3)分割2D到3D点云分割 rgb相机 https://github.com/Pointcept/SegmentAnything3D

3D框预测 VoxelNeXt - MKT

3D框预测 VoxelNeXthttps://github.com/dvlab-research/VoxelNeXt

【神器】如何查看api域名内容

查看API域名内容的方法有多种,包括使用在线工具、浏览器插件、命令行工具等。通过这些工具,你可以轻松获取API的响应数据、测试API的可用性、检查API的性能。 其中,常见的方法包括使用Postman、cURL命令行工具、浏览…

高级程序语言第二次作业

高级程序语言第二次作业这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzu/gjyycx这个作业要求在哪里 https://edu.cnblogs.com/campus/fzu/gjyycx/homework/13570学号 222200424姓名 赵伟豪目录高级程序语言第…