Bash Commands - Arithmetic Tests using (( ... ))

本文详细介绍了bash中的((...))表达式的用法,包括数值表达式、比较运算符、逻辑运算符等,并通过示例展示了如何在条件语句中应用这些表达式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The (( )) construct expands and evaluates an arithmetic expression. If the expression evaluates as zero, it
returns an exit status of 1, or "false". A non-zero expression returns an exit status of 0, or "true". This is in
marked contrast to using the test and [ ] constructs previously discussed.

#!/bin/bash

# arith-tests.sh
# Arithmetic tests.
# The (( ... )) construct evaluates and tests numerical expressions.

# Exit status opposite from [ ... ] construct!


(( 0 ))                                                                                         # 1

echo "Exit status of \"(( 0 ))\" is $?."


(( 1 ))                                                                                         # 0

echo "Exit status of \"(( 1 ))\" is $?."


(( 5 > 4 ))                                                                                   # true

echo "Exit status of \"(( 5 > 4 ))\" is $?."                          # 0


(( 5 > 9 ))                                                                                   # false

echo "Exit status of \"(( 5 > 9 ))\" is $?."                          # 1


(( 5 == 5 ))                                                                                 # true

echo "Exit status of \"(( 5 == 5 ))\" is $?."                        # 0


                                                                                                    # (( 5 = 5 )) gives an error message.
(( 5 - 5 ))                                                                                    
# 0

echo "Exit status of \"(( 5 - 5 ))\" is $?."                            # 1


(( 5 / 4 ))                                                                                      # Division o.k.

echo "Exit status of \"(( 5 / 4 ))\" is $?."                             # 0


(( 1 / 2 ))                                                                                     # Division result < 1.
echo "Exit status of \"(( 1 / 2 ))\" is $?."                           
# Rounded off to 0.
                                                                                                    
# 1
(( 1 / 0 )) 2>/dev/null # Illegal division by 0.

#
^^^^^^^^^^^
echo "Exit status of \"(( 1 / 0 ))\" is $?."
                                                                                                         # 1
# What effect does the "2>/dev/null" have?
# What would happen if it were removed?
# Try removing it, then rerunning the script.
# ======================================= #
# (( ... )) also useful in an if-then test.
var1=5
var2=4
if (( var1 > var2 ))
then                                                                      #Note: Not $var1, $var2. Why?
echo "$var1 is greater than $var2"
fi                                                                            # 5 is greater than 4

exit 0





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值