• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Make a Variable read-only (constant) in Bash and Korn Shell

by admin

Creating Bourne Shell Constants

A variable can be made read-only with the following syntax:

readonly var[=value]

The square brackets around =value mean that the assignment of a value is not always necessary. For instance, if the variable had previously been created and assigned a value, and you now want to make it read-only (and not change its current value), do not use =value.

If the variable did not previously exist, and you make it read-only, you may never assign a value to the variable. The value of a read-only variable cannot be changed. This is why read-only variables are referred to as constants:

$ sh
$ var=constant
$ readonly var
$ unset var 
var: is read only
$ var=new_value 
var: is read only

Creating Korn Shell Constants

A variable can be made read-only by using either of the following syntaxes:

typeset -r var[=value] 
readonly var[=value]

The following is a Korn shell example:

$ ksh
$ typeset -r cvar=constant
$ unset cvar 
ksh: cvar: is read only
$ cvar=new_value 
ksh: cvar: is read only

Filed Under: DevOps, Shell Scripting

Some more articles you might also be interested in …

  1. “su: Authentication failure” – in Docker
  2. The Ultimate Beginner’s Guide to Static Website Hosting With Google Firebase (for FREE)
  3. gradle: command not found
  4. Troubleshooting kubectl Error: The connection to the server x.x.x.x:6443 was refused – did you specify the right host or port?
  5. Docker Troubleshooting – “conflict: unable to delete, image is being used by running container”
  6. How to schedule master node running pod/service as a worker node
  7. How to schedule master node running pod/service as worker node
  8. How to update/add a file in the Docker Image
  9. Windows Subsystem For Linux – Run Linux on Windows (It’s FAST)
  10. 10 Sed (Stream Editor) Command Examples

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright