rubenvb
rubenvb

Reputation: 76785

How can I make a "read-only variable"?

In Bash, you can create a read-only variable

declare -r somevar='bla'

I tried to find something similar in POSIX sh, but the only thing that comes close is this phrase in the set documentation:

[...] read-only variables cannot be reset.

How can I create such a read-only variable?

Upvotes: 8

Views: 2113

Answers (1)

fedorqui
fedorqui

Reputation: 290415

You can make use of readonly:

$ var="hello"
$ readonly var
$ echo $var
hello
$ var="bye"
sh: var: readonly variable

Upvotes: 10

Related Questions