Shell Scripting – Readonly Command
Last Updated :
18 Mar, 2022
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.
Similar Reads
Shell Scripting - Set Command
The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg
7 min read
Shell Scripting - True Command
A shell provides an interface with the help of which users can interact with the system easily. To directly interact with a shell, we use an operating system. On a Unix-based operating system, every time we write a command using a terminal, we interact with the system. To interpret or analyze Unix c
3 min read
Shell Scripting - Test Command
A test command is a command that is used to test the validity of a command. It checks whether the command/expression is true or false. Here, we will walk through the syntax, key operators, and practical examples to effectively use the test command. What is the Test Command?The test command is primar
9 min read
Shell Scripting - Umask Command
A Shell script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can
3 min read
Shell Scripting - Shopt Command
Unlike many shell commands, shopt is a bash-only shell command. It is a built-in BASH shell command that controls certain options for the shell session. One can consider this as a boolean ON/OFF switch for certain shell options. The âshoptâ command provides control over many settings that are used t
7 min read
Shell Scripting - Decision Making
A Shell script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can
7 min read
Shell Scripting - Creating a Binary file
While working in Linux systems, we have used so many commands on a day-to-day basis. Most of the commands are in the binary format resides under /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, etc directories. As system administrators, we would have to write many shell scripts to do a few tasks or
4 min read
Shell Scripting - Command Substitution
A shell is an interface that helps users to connect with the system. Using a shell is equivalent to indirectly communicating with the operating system. In Linux distributed systems, each time we use the terminal, we connect with a shell. The job of a shell is to analyze Unix commands or instructions
4 min read
Shell Scripting - Define #!/bin/bash
A shell provides an interface to connect with the system. When we use an operating system, we indirectly interact with the shell. While using a terminal every time on any Linux distribution system, we interact with the shell. The main function of the shell is to interpret or analyze Unix commands. A
3 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
7 min read