[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: command substitution subshell exits when assigning to readonly varia
From: |
Greg Wooledge |
Subject: |
Re: command substitution subshell exits when assigning to readonly variable |
Date: |
Thu, 19 Oct 2023 09:18:39 -0400 |
On Thu, Oct 19, 2023 at 09:02:22AM -0400, Zachary Santer wrote:
> Here's a better way to do that:
> $ unset var
> $ var='word'
> $ if [[ ${var@a} == *'r'* ]]; then
> > printf '%s\n' "var is readonly"
And of course the *best* way would be to stop making readonly variables
in the first place. Seriously, this is a self-imposed problem.
The only reason the shell has readonly variables is to support
"restricted mode" (bash -r), where changing PATH might allow a user
to escape the restrictions. Creating a readonly variable is therefore
a *security* mechanism, meant to prevent interactive users from doing
things they're not supposed to do.
It was never intended for making "const" variables in scripts.