#!/bin/bash # # method 1
A=1234;echo "$a" | [-N "' Sed-n '/^[0-9][0-9]*$/p" "] && echo string A is numbers |
The first-n is the shell's test flag, and the subsequent string "' Sed-n '/^[0-9][0-9]*$/p '" is tested, and if not NULL, the result is true. SED shows all input line information by default, and the "-N" option for sed is to let sed not display, but only show what we need: that is, the following expression matches the row, which is implemented by adding "P" command in the expression.
/^[0-9][0-9]*$/His meaning is to match the line ## method, which is composed of at least one digit, 2, , But not bash implementation, is using GREP's regular #if grep ' ^[[:d igit:]]*$ ' <<< ' $ ';then # echo "$1 is number." #else # echo ' No. ' #fi ## method 3 #if [ "$" -gt 0 ] 2>/dev/null ;then # echo "$1 is number." #else # echo ' No. ' #fi ## method 4,case #case "$" in # [1-9 ][0-9]*) # echo "$1 is number." # ;; # *) # ;; #esac ## method 5,awk #echo $1| awk ' {print ($0~/^[-]? ( [0-9]) +[.]? ([0-9]) +$/)? " Number ":" String "} ' ## method 5,awk #if [ -n "$ (echo $1| sed -n "/^[0-9]\+$/p ")" ];then # echo "$1 is number." #else # echo ' No. ' #fi ## Method 6,expr expr $1 "+" 10 &> /dev/ null if [ $? -eq 0 ];then echo "$ is number " else echo " $1 not number " fi