DEV Community

Discussion on: Pragmatic types: dynamically-, statically-, gradually-, weakly-, strongly- and un-typed languages

Collapse
 
shalvah profile image
Shalvah

Would you say Bash is an untyped language?

Collapse
 
stereobooster profile image
stereobooster

Bash is an untyped language because it has only one type - character string, but here is mind twister which will make people doubt:

Essentially, Bash variables are character strings, but, depending on context, Bash permits arithmetic operations and comparisons on variables. The determining factor is whether the value of a variable contains only digits

People can think - "Huh it distinguishes digits, even so, they are passed around as numbers... so maybe it has types". Here is how to reason about this, take for example plus operation it takes 2 string value and returns a string

plus = (string, string) -> string

in case of two input strings contain only digits it will return another one with only digits in it (and maybe minus sign, or dot as fraction separator, but this doesn't matter for example). On the other side, a can construct function like this

#! bis/bash
pseudoPlus () {
  if [[ $1 =~ ^[0-9]*$ ]]  && [[ $2 =~ ^[0-9]*$ ]] ; 
    then echo '5';
  fi;
  echo 'something else';
}

as you can see it has same properties as plus (in case of two input strings contain only digits it will return another one with only digits in it), but it is not reasonable arithmetic operation. What matters from type system point of view is that it all variables are strings, even so, it seems that some strings have special properties (in given example they to represent numbers).

Does this explanation helps or more confuses? I'm trying to improve my skills on explaining things

Collapse
 
shalvah profile image
Shalvah

Hehe, it's okay.