emersion / emersion/mrsh

The value and readonly attribute of a variable can be overriden by getopts, read, the for loop and the `${name=word}` expansion

Open
#205 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
531
Forks
40
PR merge metrics
No merged PRs in 30d

Description

I have not observed this unique, non POSIX-compliant, behavior in ANY other shell:

```console
$ readonly a
$ readonly -p
readonly PWD=...
readonly a=''
$ read a < 1
> EOF
$ echo "$a"
1
$ readonly -p
readonly PWD=...
$ readonly a
$ readonly -p
readonly PWD=...
readonly a=''
$ for a in 1; do echo "$a"; done
1
$ readonly -p
readonly PWD=...
$ # Likewise for getopts and ${a:=word}
```

If a readonly variable can be trivially overridden like this, it is useless.

Relevant POSIX reading:

[2.5.3 Shell Variables](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_05_03):

> New variables can be defined and initialized with variable assignments, with the [read](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/read.html) or [getopts](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/getopts.html) utilities, with the name parameter in a for loop, with the ${name=word} expansion, or with other mechanisms provided as implementation extensions.

[readonly — DESCRIPTION](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_24_03)

> The values of variables with the [readonly](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#readonly) attribute cannot be changed by subsequent assignment or use of the [export](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#export), [getopts](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/getopts.html), [readonly](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#readonly), or [read](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/read.html) utilities, nor can those variables be unset by the [unset](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#unset) utility.

(The for loop is left out of the discussion, so maybe it is exempted?)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.