int-or-string.sh

来自「Shall高级编程」· Shell 代码 · 共 47 行

SH
47
字号
#!/bin/bash# int-or-string.sh: Integer or string?a=2334                   # Integer.let "a += 1"echo "a = $a "           # a = 2335echo                     # Integer, still.b=${a/23/BB}             # Substitute "BB" for "23".                         # This transforms $b into a string.echo "b = $b"            # b = BB35declare -i b             # Declaring it an integer doesn't help.echo "b = $b"            # b = BB35let "b += 1"             # BB35 + 1 =echo "b = $b"            # b = 1echoc=BB34echo "c = $c"            # c = BB34d=${c/BB/23}             # Substitute "23" for "BB".                         # This makes $d an integer.echo "d = $d"            # d = 2334let "d += 1"             # 2334 + 1 =echo "d = $d"            # d = 2335echo# What about null variables?e=""echo "e = $e"            # e =let "e += 1"             # Arithmetic operations allowed on a null variable?echo "e = $e"            # e = 1echo                     # Null variable transformed into an integer.# What about undeclared variables?echo "f = $f"            # f =let "f += 1"             # Arithmetic operations allowed?echo "f = $f"            # f = 1echo                     # Undeclared variable transformed into an integer.# Variables in Bash are essentially untyped.exit 0

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?