Skip to content

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

@9ao9ai9ar

Description

@9ao9ai9ar

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

$ readonly a
$ readonly -p
readonly PWD=...
readonly a=''
$ read a <<EOF
> 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:

New variables can be defined and initialized with variable assignments, with the read or getopts utilities, with the name parameter in a for loop, with the ${name=word} expansion, or with other mechanisms provided as implementation extensions.

readonly — DESCRIPTION

The values of variables with the readonly attribute cannot be changed by subsequent assignment or use of the export, getopts, readonly, or read utilities, nor can those variables be unset by the unset utility.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions