Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "untyped" mean the same as "dynamically typing"? [duplicate]

According to Advanced Bash-Scripting Guide,

  1. bash variables are untyped:

    Unlike many other programming languages, Bash does not segregate its variables by "type." 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.

    The link also gives examples.

    Does "untyped" mean the same as the concept of "dynamically typing" in programming languages? If not, what are the relations and differences between the two?

  2. To lighten the burden of keeping track of variable types in a script, Bash does permit declaring variables.

    For example, declare a variable to be integer type, by declare -i myvariable.

    Is this called "typed" variables? Does "typed" mean the same as the concept of "statically typing"?

like image 383
Tim Avatar asked Oct 30 '22 05:10

Tim


1 Answers

Most of this has been well answered here...

Does "untyped" also mean "dynamically typed" in the academic CS world?

by at least two people that are very familiar with the matter. To most of us that have not studied type systems etc to that level 'untyped' means dynamic typing but it's a misnomer in academic circles, see post above. untyped actually means there are no types ie think assembly, Bash is typed, it figures out it's types at runtime. Lets take the following sentence from the Advanced Bash Scripting Guide, emphasis mine...

http://tldp.org/LDP/abs/html/untyped.html

Unlike many other programming languages, Bash does not segregate its variables by "type." 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.

Bash figures out that something is a number at runtime ie it's dynamically typed. In assembler on a 64bit machine I can store any 8 bytes in a register and decrement it, it doesn't check to see if the things were chars etc, there is no context about the thing it's about to decrement it just decrements the 64 bits, it doesn't check or work out anything about the type of the thing it's decrementing.

Perl is not an untyped language but the following code might make it seem like it treats everything as integers ie

#!/usr/bin/perl
use strict;
use warnings;
my $foo = "1";
my $bar = $foo + 1;
print("$bar\n");

$foo was assigned a string but was incremented? Does this means Perl is untyped because based on context it does what you want it to do? I don't think so.

This differs from Python, Python will actually give you the following error if you try the same thing...

Traceback (most recent call last):
  File "py.py", line 2, in <module>
  bar = foo + 1

If Python is dynamically typed and Perl is dynamically typed why do we see different behavior. Is it because their type systems differ or their type conversion semantics differ. In assembly do we have type conversion instructions that change a string to an integer or vice versa?

Bash has different type conversion rules

#!/bin/bash
set -e
MYVAR=WTF
let "MYVAR+=1"
echo "MYVAR == $MYVAR";

This will assign 1 to MYVAR instead of incrementing it ie if you increment a string bash sets the string to integer zero then does the increment. It's performing type conversion which means it's typed.

For anyone still believing that Bash is untyped try this....

#!/bin/bash
declare -i var1=1
var1=2367.1

You should get something like this...

foo.sh: line 3: 2367.1: syntax error: invalid arithmetic operator (error token is ".1")

But the following shows no such error

#!/bin/bash
var1=2367.1

The output of the following

#!/bin/bash
var1=2367.1
echo "$var1"
let "var1+=1"
echo "$var1"

is the same warning without declaring a type...

2367.1
foo.sh: line 4: let: 2367.1: syntax error: invalid arithmetic operator (error token is ".1")
2367.1

A much better example is this

#!/bin/bash
arg1=1234
arg2=abc

if [ $arg1 -eq $arg2 ]; then
  echo "wtf";
fi

Why do I get this...

foo.sh: line 5: [: abc: integer expression expected

Bash is asking me for an integer expression.

like image 129
Harry Avatar answered Nov 11 '22 19:11

Harry