8

In Bash, to increase an integer value stored in a variable, we need to explicitly use arithmetic expansion.

 i=1
 i=$((i+1))

Otherwise

 i=1
 i=i+1

will not evaluate the expression i+1, but assign the expression as a string to the variable.

This is very unnatural compared to other programming languages, where an expression is evaluated by default.

In programming language design or concepts, is there a concept or terminology for such a feature in bash?

What is the point of not evaluating an expression but treating it as a string by default?

Is this feature related to the timing of evaluation, such as lazy evaluation?

Thanks.

  • who said bash was a programming language ? and no, it's not because of lazy evaluation: this exist in other languages as well without the string issue – Christophe Oct 22 '16 at 16:39
  • 8
    @Who said bash was a programming language? Well Wikipedia, Stack Overflow, reddit, wikibooks. And that's just the ones I can name before getting bored. Just like Java, there are more things wearing the name bash than just the language. But yeah, there is a bash programming language. – candied_orange Oct 22 '16 at 17:20
15

There's no special concept here. Instead, everything in the shell is a literal string, unless you explicitly use an expansion. This design decision makes it easy to assign, well, literal values, which is the primary use case of assignments:

varname=value

does not look up a variable called value, but uses the string value literally. In bash, most “special” characters like + - / etc. aren't special in any way – they are just part of literal words just like alphabetic characters.

The expansion syntax allows us to easily embed values inside a template, e.g.

echo "Hello ${name}!"

(Note that in shell, quotes like '…' or "…" do not delimit strings, instead they change which expansions are applied – single quotes suppress all expansions and word splitting, double quotes suppress word splitting and glob expansion but not variable expansion, arithmetic expansion (Bash only), or command substitution. The following words are equivalent: foo\ bar, foo' 'bar, 'foo bar'.)

A consequence of the “everything is a string by default” approach is that programs that do not only use simple strings become more complicated. Bash's array syntax is notoriously confusing. Arithmetic expansion is somewhat non-obvious. However, such programs aren't the best fit for shell scripts.

Recommended reading: The POSIX description of shell syntax and semantics: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html

7

You CAN make it work like that if you like. Everything in the shell is a literal string, unless you explicitly tell it to be something else. For example, the following prints 2:

typeset -i x
x=1
x=x+1
echo $x

You just need to tell the shell that the variable is an integer instead of a string. The same is true if you want the variable to be an array.

  • What you say is true and helpful, but does not answer the actual question, which was "what is the programming language concept [...]"? – Andres F. Nov 25 '16 at 18:58
1

It is the

string interpolation

rules for that language.

For bash the rules are that text is literal unless you use the "#{name}" format.

See also https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

0

The concept here is "backwards compatibility". The Bourne shell treated everything as strings so bash has to also by default or all pre-bash scripts would break.

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.