Bash: Check a String is a Number (whatever the format) [duplicate] - bash

This question already has answers here:
How do I test if a variable is a number in Bash?
(40 answers)
Closed 2 years ago.
I would like to share a way to check a string is a number in bash. This question has already been asked here, but the answer fits only for either integers or floats in classical format, when a number can be in scientific format, and it uses complex regex. Second, I don't have enough reputation to answer the existing question (totally fresh account here...).
So the question is:
How to (Easily) Check a String is a Number in bash?
with the string being like:
"1234"
"-1.234"
"1.23e4"
"+1.23E04"
etc.
Answer in the... answer below.

An easy way is to use awk like this:
function IsNumber {
echo "$1" | awk '{if ($1+0 == $1) print "true"; else print "false"}'
}
Examples:
$ IsNumber 1234
true
$ IsNumber 1.234
true
$ IsNumber 1.23E04
true
$ IsNumber abc
false

Related

assigning name to text file in shell script [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
I have the variable $foo="something" and would like to use:
bar="foo"; echo $($bar)
to get "something" echoed.
In bash, you can use ${!variable} to use variable variables.
foo="something"
bar="foo"
echo "${!bar}"
# something
eval echo \"\$$bar\" would do it.
The accepted answer is great. However, #Edison asked how to do the same for arrays. The trick is that you want your variable holding the "[#]", so that the array is expanded with the "!". Check out this function to dump variables:
$ function dump_variables() {
for var in "$#"; do
echo "$var=${!var}"
done
}
$ STRING="Hello World"
$ ARRAY=("ab" "cd")
$ dump_variables STRING ARRAY ARRAY[#]
This outputs:
STRING=Hello World
ARRAY=ab
ARRAY[#]=ab cd
When given as just ARRAY, the first element is shown as that's what's expanded by the !. By giving the ARRAY[#] format, you get the array and all its values expanded.
To make it more clear how to do it with arrays:
arr=( 'a' 'b' 'c' )
# construct a var assigning the string representation
# of the variable (array) as its value:
var=arr[#]
echo "${!var}"

How to do double variable substitution inside bash if loop [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
I have the variable $foo="something" and would like to use:
bar="foo"; echo $($bar)
to get "something" echoed.
In bash, you can use ${!variable} to use variable variables.
foo="something"
bar="foo"
echo "${!bar}"
# something
eval echo \"\$$bar\" would do it.
The accepted answer is great. However, #Edison asked how to do the same for arrays. The trick is that you want your variable holding the "[#]", so that the array is expanded with the "!". Check out this function to dump variables:
$ function dump_variables() {
for var in "$#"; do
echo "$var=${!var}"
done
}
$ STRING="Hello World"
$ ARRAY=("ab" "cd")
$ dump_variables STRING ARRAY ARRAY[#]
This outputs:
STRING=Hello World
ARRAY=ab
ARRAY[#]=ab cd
When given as just ARRAY, the first element is shown as that's what's expanded by the !. By giving the ARRAY[#] format, you get the array and all its values expanded.
To make it more clear how to do it with arrays:
arr=( 'a' 'b' 'c' )
# construct a var assigning the string representation
# of the variable (array) as its value:
var=arr[#]
echo "${!var}"

What does ${MY_VAR:-1} mean in bash? [duplicate]

This question already has answers here:
How does Bash parameter expansion work? [duplicate]
(2 answers)
Closed 7 years ago.
I saw this mentioned in some documentation. But it doesn't seem to do anything...
~ $ echo $DYNO
run.9917
~ $ echo ${DYNO}
run.9917
~ $ echo ${DYNO:-1}
run.9917
What does the :-1 do?
${DYNO:-1} means if DYNO exists and isn’t null, return its value; otherwise return 1. It's the :- between the variable's name and the default value inside the {} that make this happen.
This is covered in the Shell Parameter Section of the Bash Reference Manual.
"${var:-1}" means expand the parameter named var if it's defined, and if not, expand 1 instead.
Other, similar expansions:
"${var: -1}" means expand the substring of var from the last character.
"${var:=1}" means assign 1 to var if it's not defined and then, either way, expand the parameter.
Examples:
$ x=hi y=
$ echo "${x:-1}" "${x: -1}" "${y:-2}"
hi i 2
${var:-word} If var is null or unset, word is substituted for var. The value of var does not change.
$> echo "${bar:-1}"
$> 1
$> bar=3
$> echo "${bar:-1}"
$> 3
$> echo "${bar-1}"
$> 3

what is ":" called in bash? Colon usage as a pass statement [duplicate]

This question already has answers here:
What is the purpose of the : (colon) GNU Bash builtin?
(12 answers)
Closed 8 years ago.
See: What is the Bash equivalent of Python's pass statement
What is this ":" colon usage called? For example:
if [[ -n $STRING ]]; then
#printf "[INFO]:STRING: if -n string: STRING:$STRING \n"
:
else
printf "[INFO]:Nothing in the the string\n"
fi
To what that is, run help : in the shell. It gives:
$ help :
:: :
Null command.
No effect; the command does nothing.
Exit Status:
Always succeeds.
Very useful in one-liner infinite loops, for example:
while :; do date; sleep 1; done
Again, you could write the same thing with true instead of :, but this is shorter.
Interestingly:
$ help true
true: true
Return a successful result.
Exit Status:
Always succeeds.
According to this, the difference is that : is "Null command",
while true is "Returns a successful result".
Another difference is that true is usually a real binary:
$ which true
/usr/bin/true
On the other hand, which : gives nothing. (Which makes sense, being a "null command".)
Anyway, #Andy is right, this is duplicate of this other post, which explains it much better.

Bash - variable variables [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
I have the variable $foo="something" and would like to use:
bar="foo"; echo $($bar)
to get "something" echoed.
In bash, you can use ${!variable} to use variable variables.
foo="something"
bar="foo"
echo "${!bar}"
# something
eval echo \"\$$bar\" would do it.
The accepted answer is great. However, #Edison asked how to do the same for arrays. The trick is that you want your variable holding the "[#]", so that the array is expanded with the "!". Check out this function to dump variables:
$ function dump_variables() {
for var in "$#"; do
echo "$var=${!var}"
done
}
$ STRING="Hello World"
$ ARRAY=("ab" "cd")
$ dump_variables STRING ARRAY ARRAY[#]
This outputs:
STRING=Hello World
ARRAY=ab
ARRAY[#]=ab cd
When given as just ARRAY, the first element is shown as that's what's expanded by the !. By giving the ARRAY[#] format, you get the array and all its values expanded.
To make it more clear how to do it with arrays:
arr=( 'a' 'b' 'c' )
# construct a var assigning the string representation
# of the variable (array) as its value:
var=arr[#]
echo "${!var}"

Resources