How to use the command 'readonly' (with examples)

How to use the command 'readonly' (with examples)

The readonly command is used to create or modify read-only variables within a shell script. By making a variable read-only, it prevents the variable from being changed by subsequent commands. This is particularly useful when you want to ensure that a variable retains a constant value throughout the execution of a script.

Use case 1: Create a read-only variable

Code:

readonly variable_name=value

Motivation: In certain situations, you may want to create a variable in a shell script and ensure that its value remains constant throughout the script execution. By using the readonly command, you can create a read-only variable, providing an initial value that cannot be modified further in the script.

Explanation:

  • readonly: This is the command itself, indicating that the variable should be read-only.
  • variable_name: The name of the variable you want to create.
  • value: The initial value you want to assign to the variable.

Example Output:

readonly GREETING="Hello, world!"
echo $GREETING

Output:

Hello, world!

In the above example, we create a read-only variable named GREETING with an initial value of “Hello, world!”. Since the variable is marked as read-only, any attempt to change its value in subsequent commands will result in an error.

Use case 2: Mark a variable as read-only

Code:

readonly existing_variable

Motivation: In some cases, you may have already defined a variable in your script and realize later that it should not be modified. Instead of rewriting the entire variable declaration, you can use the readonly command to mark the existing variable as read-only.

Explanation:

  • readonly: This is the command itself, indicating that the variable should be read-only.
  • existing_variable: The name of the variable you want to mark as read-only.

Example Output:

existing_variable="This variable can be modified"
readonly existing_variable
existing_variable="This modification will result in an error"

Output:

readonly: existing_variable: readonly variable

In the above example, we first assign a value to the variable existing_variable. However, after marking it as read-only, any attempt to modify its value will result in an error.

Use case 3: [p]rint the names and values of all read-only variables to stdout

Code:

readonly -p

Motivation: During the execution of a shell script, you may want to check the values of read-only variables. The readonly -p command allows you to print the names and values of all read-only variables to standard output (stdout).

Explanation:

  • readonly: This is the command itself, indicating that the variable is read-only.
  • -p: This option is used to print the names and values of all read-only variables.

Example Output:

readonly NAME="John Doe"
readonly AGE=30
readonly GENDER="Male"
readonly -p

Output:

readonly NAME='John Doe'
readonly AGE='30'
readonly GENDER='Male'

In the above example, we define three read-only variables: NAME, AGE, and GENDER. The readonly -p command prints the names and values of all read-only variables, providing a useful way to verify the values of these variables during script execution.

Conclusion:

The readonly command is a useful tool for creating and modifying read-only variables within a shell script. By using this command, you can ensure that the values of specific variables remain constant throughout the script execution, preventing unintended changes. Additionally, the readonly -p option allows you to print the names and values of all read-only variables, providing a convenient way to verify their values during script execution.

Related Posts

How to use the command ogrinfo (with examples)

How to use the command ogrinfo (with examples)

The ogrinfo command is a powerful tool in the GDAL (Geospatial Data Abstraction Library) that allows users to list information about an OGR-supported data source.

Read More
How to use the command jcal (with examples)

How to use the command jcal (with examples)

The jcal command is used to display calendar information in the Jalali format, with the current day highlighted.

Read More
How to use the command xbmtopbm (with examples)

How to use the command xbmtopbm (with examples)

The xbmtopbm command is used to convert X11 or X10 bitmaps to PBM (Portable Bitmap) images.

Read More