Readonly command

From Linux Shell Scripting Tutorial - A Beginner's handbook
Revision as of 22:49, 29 March 2016 by Admin (talk | contribs) (Text replacement - "<source lang="bash">" to "<syntaxhighlight lang="bash" >")
Jump to navigation Jump to search

Make variable readonly

<syntaxhighlight lang="bash" >readonly var readonly var=value readonly p=/tmp/toi.txt

  1. error


Make function readonly

  • You need to use the -f option to make corresponding function readonly and syntax is:

<syntaxhighlight lang="bash" >readonly -f functionName</source>

  • For example, write a function called hello() at a shell prompt, enter:

<syntaxhighlight lang="bash" >function hello() { echo "Hello world"; }

  1. invoke it


  • Make it readonly:

<syntaxhighlight lang="bash" >readonly -f hello

  1. invoke it


  • Now, try to update the hello(), enter:

<syntaxhighlight lang="bash" >function hello() { echo "Hello $1, let us be friends."; }</source> Sample outputs:

bash: hello: readonly function

Display all readonly variables

If no arguments are given, or if -p is given to the readonly buitin, a list of all readonly names is printed on screen: <syntaxhighlight lang="bash" >readonly</source> OR <syntaxhighlight lang="bash" >readonly -p</source> Sample outputs:

declare -ar BASH_VERSINFO='([0]="3" [1]="2" [2]="39" [3]="1" [4]="release" [5]="i486-pc-linux-gnu")'
declare -ir EUID="1000"
declare -ir PPID="7628"
declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"
declare -ir UID="1000"
declare -r pwdfile="/etc/passwd"

Display all readonly functions

Type the following command: <syntaxhighlight lang="bash" >readonly -f</source> Sample outputs:

hello () 
    echo "Hello world"
declare -fr hello

See also