Open In App

Shell Scripting – Readonly Command

Last Updated : 18 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this tutorial, we will learn about the ‘readonly’ command. 

Using the readonly command, you can assign a particular value to a variable that can neither be changed nor modified by subsequent assignments nor be unset. 

Syntax:

readonly name[=value]    
readonly [-aAf] [name[=value] 

Most used options:

-p : It prints all the defined readonly variables
-f : It is used when you want to make a function as readonly  

To get further help with its usage you can use the command:

readonly --help

Example of the usage:

  • Navigate to the terminal and create a file with the name: “gfg.sh” using the command
touch gfg.sh
  • Open the vim editor by using the command:
vim gfg.sh
  • Now to go to the insert mode, press ‘i’ on the keyboard.

Let’s now start writing a shell script where we will first assign a value to a variable, make it readonly, and then try to change its value again.

#! /bin/bash
echo "Learning shell commands"

# defining a variable "myvar"
myvar="geeksforgeeks"
echo " The value variable 'myvar' currently is: $myvar"

# making that variable as read only
readonly myvar
echo "Hold on! Trying to change the value of 'myvar'....."

# trying to change the variable
myvar="gfg"

  • Now to save it press Esc to exit the insert mode then type ‘:w’ and it will save the script. After saving, the last line of the file will look like this:

  • Now simply exit the editor by typing “:q”.
  • Test the script by using the command:
bash gfg.sh  

After you run the script you can see that you will get an error at line 10, where we tried to change the value of the variable.

Additionally, you can also use -r flag along with the declare command to set a variable as read-only.  To learn more about the ‘declare’ command, refer to this article: declare command in Linux with Examples

Now let us declare some readonly functions:

If you will now run the below command:

readonly -f  

It will display all the functions which are marked as read-only

Similarly, If you run the command:

readonly or readonly -p  

Then it will list all the variables with their values which are defined as read-only (this includes default read-only variables)

Unsetting or modifying a readonly variable

In the example script ‘gfg.sh’ above, we have seen that the value of a variable marked as readonly cannot be modified. 

Additionally, the value of readonly variable cannot be unset by using the ‘unset’ command. 

Setting a variable as readonly helps you protect your variable from any further modification and improves code readability. Note that when the variable marked as readonly in a session is exported in another session, its value can be changed.

When we tried to modify the value of variable “myvar” in the current shell (Shell with PID as: 1618) it gave an error. But when we exported this variable to another session (PID: 1624), its value was successfully changed or modified.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads