1

I've written a simple script that checks if a given number is float. But it isn't working quite opposite. Here's the code:

#! /bin/bash

main () {
  if [[ "$1" == ^[0-9]+\.?[0-9]+ ]]
  then
    echo "Floating number"
  else
    echo "Not a floating number"
  fi
}

main "$@"

When I'm giving a floating-point number, it's executing else part. Here's the output:

Output

Sarat Kota
  • 43
  • 8
  • Note that the question I closed this as a duplicate of explicitly has answers covering floating-point using regexes, so even though its title is less explicit, it does indeed cover the relevant space. – Charles Duffy Feb 19 '21 at 16:38
  • As a one-sentence answer: `==` is a glob-style match, not a regex match; use `=~`. (Also, `==` shouldn't be used in a non-arithmetic operator in bash at all; the POSIX-standard string comparison operator is `=`; using it even in `[[` helps keep finger memory for writing correct code for `/bin/sh`). – Charles Duffy Feb 19 '21 at 16:38
  • Also -- post the output as text, same as how you posted the code. See [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) on [meta]. – Charles Duffy Feb 19 '21 at 16:40
  • Thank you, Charles! I will not upload images going further. – Sarat Kota Feb 20 '21 at 06:00

0 Answers0