首页 > 解决方案 > 在鱼壳中如何检查变量是否为数字?

问题描述

如何检查变量是否是鱼壳中的数字?寻找一种简单的方法。

标签: shellfish

解决方案


一种可能的解决方案是使用string match如下:

set num 123
if string match -qr '^[0-9]+$' $num; echo 'it is a number!'; end

如果num不是数字,则不会回显任何内容。

这是string match鱼壳文档(https://fishshell.com/docs/current/commands.html#string)的概要:

string match [(-a | --all)] [((-e | --entire)] [(-i | --ignore-case)] [(-r | --regex)]
         [(-n | --index)] [(-q | --quiet)] [(-v | --invert)] PATTERN [STRING...]

推荐阅读