linux 脚本 “=~” 表达 意思

1、在shell脚本中,经常遇到 “=~” ,表达形式如下:

[[ "string" =~ pattern ]]

 “=~” 用于判断string和右边的正则表达式pattern是否匹配

2、shell 脚本

[root@master ~]# cat hh.sh
#!/bin/bash
INT=-15a
if [[ "$INT" =~ ^-?[0-9]+$ ]]; then
echo "INT is an integer."
else
echo "INT is not an integer." >&2
exit 1
fi
[root@master ~]# sh hh.sh
INT is not an integer.

ps:正则表达式^表示起始,$表示结束,?表示0个或者1个

参考:

https://stackoverflow.com/questions/12454731/meaning-of-operator-in-shell-script

https://unix.stackexchange.com/questions/340440/bash-test-what-does-do

猜你喜欢

转载自blog.csdn.net/Man_In_The_Night/article/details/86671974