0

I was surprised to read this in the Bash manual, describing Arithmetic Evaluation:

Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax.

I tried it out, and indeed, var behaves the same as $var:

❯ var=9
❯ echo $(( var + 5 ))
14
❯ (( var < 10 )) && echo 'single digits!'
single digits!

Is it considered bad practice to do so? Is it portable to other shells, or is this a Bashism?

1 Answer 1

2

It is not considered bad practice to not use $ for variables inside of arithmetic evaluation. As far as portability, ((…)) in general is not POSIX, so it will not work in shells such as dash. It will work in zsh and ksh. On the other hand, $((…)) is in POSIX and works in all modern sh-style shells.

1
  • Side note: most bashisms are copied from ksh. Commented Jun 13, 2017 at 0:51

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.