-2

Possible Duplicate:
How do I test if a variable is a number in bash?

I am new to bash scripts. Needed a code to check whether a variable is a number, if YES i have to keep it as it, if NO i ll have to reassign it to some other value. How do I get this done?

1
  • 1
    Some particular number or that it's not a non-number? Integer or float? What have you tried? Commented Aug 30, 2012 at 14:27

1 Answer 1

0

Your basic if () then ... fi. Here's the format:

#!/bin/bash

var=2

if [[ $var != 1 ]]
then
    var=5;
fi

echo $var

Note that the [[...]] format can actually be one of many things.

As with everything, if you want to get good at it, you'll have to work for it. Type

  • man bash
  • man test

to get the manuals on the tools you're using. Read them top to bottom, again, again, and once more. And then tomorrow the same thing.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.