M
M
Mikhail Moskalev2014-04-02 12:03:47
bash
Mikhail Moskalev, 2014-04-02 12:03:47

Bash - how to convert string to number?

Good afternoon! It is necessary to run different scripts with one command depending on the time of day.
To do this, I get hours using date
hour = date +%H
And then I try to compare with a number

if [  hour < 20 ]; then echo 'true'; else echo 'false'; fi

But since hour is a string, the comparison fails.
How can I convert a number to a string correctly? Or how can this problem be solved in another way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Zhivotnev, 2014-04-02
@inkvizitor68sl

Bash has no concept of strings/numbers. If there is a number inside, then it will parse this number.
Well, in the example from the solution, it is better to write ${todate}. And in general, it is always better to write variables in ${} - this is readable and saves you from a lot of errors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question