4

Is there a shell that supports distinguishing between variable types and multidimensional arrays?

At a minimum, it should distinguish between Strings, Integers, Floats and Doubles.

The following commands should be different. command 1 and command "1" if variable types were supported. Notice it would be similar to a c function like fun(1) and fun("1"). Alternatively it should have 2 separate syntaxes for shell and calling functions.

16
  • Bash can distinguish between strings and integers.
    – muru
    Oct 17 '15 at 2:26
  • @muru True but not when passing arguments into a command. In addition it only really supports 2 types Strings and Numbers, which is to narrow according to my question.
    – William
    Oct 17 '15 at 2:29
  • *integers, not numbers. Your question is about variables. Arguments to a command can be the content of variables, so I don't see what you mean by the first sentence.
    – muru
    Oct 17 '15 at 2:31
  • 2
    Is that a fact? Anyway if it's the case that eventually they become strings, why should the shell take the trouble of parsing them?
    – muru
    Oct 17 '15 at 2:39
  • 1
    cygwin sucks. but, since we're kind of one the subject, Korn's group is also responsible for UWIN.
    – mikeserv
    Oct 20 '15 at 4:48
4

Perl Shell supports three types of variables: scalars, lists, and hashes.

Perl Shell (psh) combines aspects of bash and other shells with the power of Perl scripting.

and

The Perl Shell is a shell that combines the interactive nature of a Unix shell with the power of Perl. The goal is to eventually have a full featured shell that behaves as expected for normal shell activity. But, the Perl Shell will use Perl syntax and functionality for control- flow statements and other things.

It doesn't so much distinguish between Strings, Integers, Floats and Doubles[1], as seamlessly convert between them as needed, exactly as Perl does.

It's difficult to see any reason why a shell would need to distinguish between a number 1 and the string "1" in any situation where auto-coversion was the Wrong Thing To Do, or where different operators for string vs numeric vs regexp comparison didn't suffice. Much like bash or other shells do.

[1] See specialist modules like Math::BigFloat if special handling of float types is required. or Math::Bigint for very large integers.

11
  • 3
    finally! a question to which psh is an answer!!
    – cas
    Oct 17 '15 at 3:42
  • Does this run on Windows?
    – William
    Oct 20 '15 at 3:41
  • no idea, i rarely use windows. i would have thought that Powershell would be more appropriate on windows.
    – cas
    Oct 20 '15 at 5:03
  • Why doesn't something like the following work $test="test";echo $test? I don't see the advantage of perl if you can't pass variables as arguments.
    – William
    Nov 21 '15 at 23:04
  • what did i say that makes you think that? it's certainly not true.
    – cas
    Nov 21 '15 at 23:13
4

There's a tension between having fine distinction between types and allowing quick typing. Shells are strongly geared towards quick typing: their purpose is to let you do once-off stuff easily, in as few characters as possible. Having to add quotes because you want 1 to be a string (to pass it to a program) rather than a number would be a burden.

Ksh, bash and zsh do have typed variables. However, these variables are just strings with extra validation. If you try to set an integer variable to a value that isn't an arithmetic expression, you'll get an error, or sometimes the value 0 (and in particular any valid identifier is replaced by 0 if it isn't a defined variable, unless set -u is in effect).

$ integer n
$ n=3
$ echo $n
3
$ n=2+2
$ echo $n
4
$ n=undefined+1
$ echo $n
1
$ n='_!@()#(*#'
mksh: _!@()#(*#: unexpected '!'

Ksh93 goes further and allows you to define custom types (with typeset -T), with functions to get and set variable values (called discipline functions) that allow you to reject invalid values. There's a predefined helper enum to define a type with a finite number of allowed values.

$ enum day=(Sunday Monday Tuesday Wednesday Thursday Friday)
$ day d=Monday
$ echo $d
Monday
$ d=monday
ksh93: d:  invalid value monday

All values can be transparently used as strings though. If you don't want that, you automatically reduce the value of the language as a shell, because in the end the primary purpose of a shell is to run other programs, and a program's command line arguments are strings. However, there are interactive environments in which launching other programs is not considered the primary task, which don't have automatic conversion to strings. I list some in Object-oriented shell for *nix

2
  • It doesn't appear multiple dimensional arrays are supported is that correct? Also do you mind posting a Double and Float example?
    – William
    Oct 20 '15 at 3:48
  • Ksh93 and zsh have floats (float x), bash doesn't. You only get the maximum precision supported by the implementation (what C calls double or long double). When it comes to compound data structures, there are only arrays and string→string maps (which shells call associative array), in particular none give you nested arrays. Look at the documentation of the typeset builtin (declare in bash) to see what types each shell supports. Oct 20 '15 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.