Difference between revisions of "Readonly command"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
Line 47: Line 47:
declare -fr hello
declare -fr hello
==See also==
* [[Declare command]]
* [[Variables|Variables in shell]]

Revision as of 08:58, 25 September 2009

Make variable readonly

readonly var
readonly var=value
readonly p=/tmp/toi.txt
# error

Make function readonly

  • You need to use the -f option to make corresponding function readonly and syntax is:
readonly -f functionName
  • For example, write a function called hello() at a shell prompt, enter:
function hello() { echo "Hello world"; }
# invoke it 
  • Make it readonly:
readonly -f hello
# invoke it 
  • Now, try to update the hello(), enter:
function hello() { echo "Hello $1, let us be friend"; }

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:



readonly -p

Sample outputs:

vivek@vivek-desktop:~/pdfs$ readonly 
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:

readonly -f

Sample outputs:

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

See also