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
6 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 - 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 - 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
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
8 min read
Bash Scripting - Bash Echo Command
In this article, we are going to see the echo command. The Echo command is a built-in command feature for Unix / Linux which is generally used to display the text or message on the screen. Syntax : $ echo [option][text]For Example : $ echo Geeks For GeeksOutput : Geeks For GeeksOutputThere are gener
2 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
Shell Scripting - Restricted Shell
Shell is one of the most important and powerful tools available in GNU/Linux-based systems. One can control the entire system if used correctly, this exact feature makes the shell a double-edged sword. It can potentially harm the system if one executes a system command without knowing its underlying
5 min read
Shell Scripting - Interactive and Non-Interactive Shell
A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell
3 min read