Difference between revisions of "Readonly command"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
==Make variable readonly==
 
==Make variable readonly==
<source lang="bash">readonly var
+
<syntaxhighlight lang="bash" >readonly var
 
readonly var=value
 
readonly var=value
 
readonly p=/tmp/toi.txt
 
readonly p=/tmp/toi.txt
 
# error
 
# error
p=/tmp/newvale</source>
+
p=/tmp/newvale</syntaxhighlight>
 
==Make function readonly==
 
==Make function readonly==
 
* You need to use the -f option to make corresponding function readonly and syntax is:
 
* You need to use the -f option to make corresponding function readonly and syntax is:
<source lang="bash">readonly -f functionName</source>
+
<syntaxhighlight lang="bash" >readonly -f functionName</syntaxhighlight>
 
* For example, write a function called hello() at a shell prompt, enter:
 
* For example, write a function called hello() at a shell prompt, enter:
<source lang="bash">function hello() { echo "Hello world"; }
+
<syntaxhighlight lang="bash" >function hello() { echo "Hello world"; }
 
# invoke it  
 
# invoke it  
hello</source>
+
hello</syntaxhighlight>
 
* Make it readonly:  
 
* Make it readonly:  
<source lang="bash">readonly -f hello
+
<syntaxhighlight lang="bash" >readonly -f hello
 
# invoke it  
 
# invoke it  
hello</source>
+
hello</syntaxhighlight>
 
* Now, try to update the hello(), enter:
 
* Now, try to update the hello(), enter:
<source lang="bash">function hello() { echo "Hello $1, let us be friend"; }</source>
+
<syntaxhighlight lang="bash" >function hello() { echo "Hello $1, let us be friends."; }</syntaxhighlight>
 
Sample outputs:
 
Sample outputs:
 
<pre>bash: hello: readonly function</pre>
 
<pre>bash: hello: readonly function</pre>
Line 25: Line 25:
 
==Display all readonly variables==
 
==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:
 
If no arguments are given, or if -p is given to the readonly buitin, a list of all readonly names is printed on screen:
<source lang="bash">readonly</source>
+
<syntaxhighlight lang="bash" >readonly</syntaxhighlight>
 
OR
 
OR
<source lang="bash">readonly -p</source>
+
<syntaxhighlight lang="bash" >readonly -p</syntaxhighlight>
 
Sample outputs:
 
Sample outputs:
 
<pre>readonly  
 
<pre>readonly  
Line 39: Line 39:
 
==Display all readonly functions==
 
==Display all readonly functions==
 
Type the following command:
 
Type the following command:
<source lang="bash">readonly -f</source>
+
<syntaxhighlight lang="bash" >readonly -f</syntaxhighlight>
 
Sample outputs:
 
Sample outputs:
 
<pre>hello ()  
 
<pre>hello ()  

Latest revision as of 22:50, 29 March 2016

Make variable readonly

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

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 
hello
  • Make it readonly:
readonly -f hello
# invoke it 
hello
  • Now, try to update the hello(), enter:
function hello() { echo "Hello $1, let us be friends."; }

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

OR

readonly -p

Sample outputs:

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