Modifying a read-only variable

Last update on August 13, 2009 11:05 AM by jak58
Published by netty5

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

$
Best answers for « Modifying a read only variable » in :
Download Total Video Converter Show Description: Total Video Converter is a converter that converts video and audio files. It will allow you to convert your video files for your mobile phones, PSP, PS3, iPod, iPhone, etc. It can read various video file formats like mpeg, avi, mov, flv...
How to read a RealMedia Variable (RMVB) Bitrate file ShowHow to read a RealMedia Variable (RMVB) Bitrate file What is a RMVB file? How to read a RealMedia Variable Bitrate file Convert a RealMedia File Using EO Video What is a RMVB file? RMVB is the acronym used for RealMedia Variable...
What is an MKV file and how to read it ShowWhat is an MKV file and how to read it What is an MKV file? An MKV file is an audio and video container that can support various audio, video and subtitle compression formats. MKV files usually have a menu system slightly similar to a DVD menu,...
[VBA/VB6] My Documents + Environment Variables Show[VBA/VB6] My Documents + Environment Variables With VBA With VB6 Environment Functions Windows Variables Getting started As displayed in Windows Explorer, the My Documents folder appears to be in the root, but it is not the case. It...
Download Adobe Reader SpeedUp ShowAdobe Reader SpeedUp is a tool that enables you to optimize opening documents by unused plugins temporarily as it delays access to Adobe documents. Simple and easy to use, the application has been awarded from various popular places. Features Non...
Download Cool PDF Reader ShowDescription The application is designed by PDFEXESoftware. Cool PDF Reader is a tool that enables you to read and convert PDF file to various different formats. Simple and easy to use, the application has been awarded from several popular places. The...
Download Acrobat Reader ShowAcrobat Reader is the original software of the company Adobe allowing to read and to print documentations in PDF format. PDF files are faithful files to the original documents and the format guarantees the protection of information of the source...
Environment variables ShowEnvironment variables An environment variable is a dynamic value loaded into the memory that can be used by several processes operating simultaneously. On most operating systems, the location of some libraries or of the main system executables may...
Read-only memory (ROM) ShowRead-only memory (ROM) There is a type of memory that stores data without electrical current; it is the ROM (Read Only Memory) or is sometimes called non-volatile memory as it is not erased when the system is switched off. This type of memory...
PC Assembly - Getting the case ready ShowGetting the casing ready When preparing to build a PC make sure to have the following: a phillips screwdriver, the necessary screws and the various cables and connectors. There are some simple guidelines that should be followed: Never screw...