What is the programming language concept for not evaluating an expression by default in bash?

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.

2

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

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.

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

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.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *