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

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • 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. Run Docker as a non-root user
  2. How to Trace Python Scripts using trace.py
  3. How To Change The Time Zone For A Docker Container
  4. How to use ansible-config to discover and investigate configuration options
  5. How to Create a Public/Private Repository in Docker Hub and connect it remotely using command line
  6. “while” Loop Examples in Shell Scripts
  7. New User Failed Run Kubectl with Error “The connection to the server xxx.xxx.xxx was refused – did you specify the right host or port?”
  8. How to change the default IP address of docker bridge
  9. How to Configure Btrfs as the Storage Engine in Docker
  10. How to write multiple plays and per-play privilege escalation in Ansible

You May Also Like

Primary Sidebar

Recent Posts

  • How to disable ICMP redirects on CentOS/RHEL
  • What are Oracle Key Vault Roles
  • What Is Oracle Key Vault
  • Auditing with Oracle Database Vault Reports
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary