Join my Bash scripting course on Udemy 👀

Data types in bash scripts

May 11, 2021  ‐ 1 min read

Whether you pass data to a script as an argument or assign values to a variable. Bash is not that aware of data types, bash treats it all as strings. However, bash may allow arithmetic operations and comparisons on variables based on the context in which the variable is used.

The fact that bash does not differentiate between strings and integers or floats allows you to be flexible in your shell scripts. Be aware of subtle errors though.

In practice you can enforce an integer of you like by using the command declare with -i as an options. However, this isn't used that often in shell scripts.

declare -i week
week=7

More effectively, if you need a numeric value you can test it in an if-statement.

if ! [[ "$week" =~ ^[0-9]+$ ]];; then
  echo "Week is not numeric"
fi