Create the constants variable

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← Create an integer variableHomeBash variable existence check →
readonly var
readonly varName=value
  • The declare builtin syntax is as follows:
declare -r var
declare -r varName=value

Example

  • Create a constant variable called DATA and make its value always the same throughout the shell script i.e. it can't be changed:
readonly DATA=/home/sales/data/feb09.dat
echo $DATA
/home/sales/data/feb09.dat
DATA=/tmp/foo
# Error ... readonly variable
  • You cannnot unset (delete) the readonly variable:
unset DATA

Sample outputs:

bash: unset: DATA: cannot unset: readonly variable
← Create an integer variableHomeBash variable existence check →