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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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. How To Get Information About a Container In Docker
  2. What is the purpose of .bash_profile file under User Home Directory In Linux
  3. Run Docker as a non-root user
  4. Windows Subsystem For Linux – Run Linux on Windows (It’s FAST)
  5. How to List / Start / Stop / Delete docker Containers
  6. How to Create a MySQL Docker Container for Testing
  7. How to update/add a file in the Docker Image
  8. My Development Environment Set up on Windows to use Python for Web Dev & Data Science
  9. Understanding Variables in Bash Shell Under Linux
  10. How to Disable Docker Process and docker0 Interface on CentOS/RHEL

You May Also Like

Primary Sidebar

Recent Posts

  • JavaFX ComboBox: Set a value to the combo box
  • Nginx load balancing
  • nginx 504 gateway time-out
  • Images preview with ngx_http_image_filter_module

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright