Cyberithub

How to declare a variable read only in Linux Bash Shell Scripting

Advertisements

In this article, we will see how to declare a variable read only in Linux Bash Shell scripting. Like in many other programming and scripting languages there might be a situation in Linux bash shell scripting as well where you need to declare a variable as read only so that value assigned to it cannot be changed. In other words, you need to protect the read only variable from getting overridden. While there are many ways to protect the read only variable in Linux but here we will only discuss the most frequently and famous methods used that can be easily used with terminal as well as from the bash shell scripting.

How to declare a variable read only in Linux Bash Shell Scripting

How to declare a variable read only in Linux Bash Shell Scripting

Also Read: 7 Bash Shell Scripts Every Linux Administrators Should Know About

Advertisements

If you are looking to declare a variable as read only in Linux then there are few methods that you can think of using here. We will see all the methods one by one in great detail with the help of example.

Method 1: Using readonly command

The first method that you can think of using is through readonly command where a variable declared with a value assigned can be changed to different value. This can be better understood with an example. Suppose you have declared a variable called color with value red assigned to it as shown below.

Advertisements
cyberithub@ubuntu:~$ readonly color=red

Now if you check the value of color variable using echo $color, you can see red on the output.

cyberithub@ubuntu:~$ echo $color
red

Next if you try to assign some other value like yellow to color variable using below set command. Then it won't be assigned.

Advertisements
cyberithub@ubuntu:~$ set color=yellow

This can be confirmed by again checked the value assigned to color variable using echo $color command. You will notice that color variable still contains the old value red.

cyberithub@ubuntu:~$ echo $color
red

You can even verify by unsetting the old value from color variable using unset color command. You will notice -bash: unset: color: cannot unset: readonly variable message on the output. This confirms that color variable is read only and hence the assigned value cannot be changed.

Advertisements
cyberithub@ubuntu:~$ unset color
-bash: unset: color: cannot unset: readonly variable

 

Method 2: Using typeset command

Another method that can be used is through typeset command. You just need to use typeset -r <var>=<value> syntax. You can declare a variable called color with value red assigned to it using typeset -r color=red command.

cyberithub@ubuntu:~$ typeset -r color=red

Then you can check the value assigned using echo $color command.

cyberithub@ubuntu:~$ echo $color
red

Now if you try to change the value to yellow then you can notice that it is throwing -bash: typeset: color: readonly variable message on the output. This confirms that color is a read only variable.

cyberithub@ubuntu:~$ typeset -r color=yellow
-bash: typeset: color: readonly variable

 

Method 3: Using declare command

Last method is through the use of declare command where you can assign a value to a variable and declare it read only. The syntax that you need to use is declare -r <variable>=<value>. This can be better understood with the help of an example. Here we are setting the value of a variable color is equal to red as shown below.

cyberithub@ubuntu:~$ declare -r color=red

You can check the value assigned to color variable by using echo $color command as shown below.

cyberithub@ubuntu:~$ echo $color
red

Now if you try to set the value to yellow then you will get below shown error.

cyberithub@ubuntu:~$ declare -r color=yellow
-bash: declare: color: readonly variable

This confirms that variable color is now defined as read only variable whose value cannot be changed. You can also check the type of color variable by simply using declare -p color command as shown below.

cyberithub@ubuntu:~$ declare -p color
declare -r color="red"

If you use declare -p without any argument then you will see all the declared variables as shown below.

cyberithub@ubuntu:~$ declare -p
declare -- BASH="/bin/bash"
declare -r BASHOPTS="checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath"
declare -i BASHPID
declare -A BASH_ALIASES=()
declare -a BASH_ARGC=([0]="0")
declare -a BASH_ARGV=()
declare -- BASH_ARGV0
declare -A BASH_CMDS=()
declare -- BASH_COMMAND
declare -a BASH_COMPLETION_VERSINFO=([0]="2" [1]="10")
declare -a BASH_LINENO=()
declare -a BASH_SOURCE=()
declare -- BASH_SUBSHELL
declare -ar BASH_VERSINFO=([0]="5" [1]="0" [2]="17" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
declare -- BASH_VERSION="5.0.17(1)-release"
declare -- COLUMNS="157"
declare -- COMP_WORDBREAKS
declare -x CONDA_EXE="/home/cyberithub/miniconda3/bin/conda"
declare -x CONDA_PYTHON_EXE="/home/cyberithub/miniconda3/bin/python"
declare -x CONDA_SHLVL="0"
..................................................

Leave a Comment