Artificial intelligent assistant

Assigning value to readonly variable during declaration Is the following type of `readonly` variable definition valid?: #!/bin/sh readonly a=1 readonly b=1 readonly c=$((a+b)) Is this type of declaration in combination with assignment allowed/recommended?

You are allowed to use the read-only variables `a` and `b` to initialize the read-only variable `c` like you're showing in your question.

Generally though, you'd want to use


readonly c="$(( a + b ))"


i.e. quoting the expansion. If the variable `IFS` has a value that includes the digit `2`, it could otherwise lead to an empty value in `c`. The value would be empty as the shell would have split the arguments to `readonly` on the `2`, resulting in the command


readonly c=


being executed.

Example:


$ cat script
#! /bin/sh

IFS=2
readonly a=1
readonly b=1
readonly c=$((a+b))



$ yash -o posixly-correct -x script
+ IFS=2
+ readonly 'a=1'
+ readonly 'b=1'
+ readonly 'c='


Note that all shells don't work like this though:


$ bash --posix -x script
+ IFS=2
+ readonly a=1
+ a=1
+ readonly b=1
+ b=1
+ readonly c=2
+ c=2

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy c35256b20f0044f38f8e45dea2b02199