0
Thanks

A few words of thanks would be greatly appreciated.

Modifying a read-only variable






Solution 1


A variable declared as read-only can not be modified during outgoing process.
But if the variable is exported in a child shell it may be modified.

#!/bin/bash

if [ "$PROC_PID" != "$PPID" ]; then
  export PROC_PID=$$
  var="mype"
  echo "initial $var"

  declare -r var

  export var
  $0 & # child process 
else
  echo "before $var"
  var="netty5"
  echo "after $var"
fi

Solution 2


Making use of indirect development.

# Declaration of read-only Variable
$ readonly Z="Y"
$ echo $Z
Y

# Suppression test 
$ unset Z
-l: unset: Z: cannot unset: readonly variable

# Replacement test
$ Z=W
-l: Z: readonly variable

# Declaration of an indirect variable
$ Y=W
$ echo "$Y"
W

# Development of variable with indirection
$ echo "${!Z}"
W

$
0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Related

This document, titled "Modifying a read-only variable," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (https://ccm.net/).

0 Comments