Difference between revisions of "$0"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
m (Text replacement - "<source lang="bash">" to "<syntaxhighlight lang="bash" >")
Line 1: Line 1:
 
'''$0''' is one of the [[Bash special parameters]]. It can only be referenced as follows (just an example as there are various ways to reference and use $0 while scripting) :
 
'''$0''' is one of the [[Bash special parameters]]. It can only be referenced as follows (just an example as there are various ways to reference and use $0 while scripting) :
<source lang="bash">echo "$0"
+
<syntaxhighlight lang="bash" >echo "$0"
 
echo "Usage: $0 fileName"</source>
 
echo "Usage: $0 fileName"</source>
 
However, assignment to it is not allowed:
 
However, assignment to it is not allowed:
<source lang="bash">0=foo</source>
+
<syntaxhighlight lang="bash" >0=foo</source>
 
== Purpose ==
 
== Purpose ==
 
$0 expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. It is often used to display script usage message:
 
$0 expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. It is often used to display script usage message:
<source lang="bash">#!/bin/bash
+
<syntaxhighlight lang="bash" >#!/bin/bash
 
_file="$1"
 
_file="$1"
  
Line 22: Line 22:
 
echo "Processing $_file..."</source>
 
echo "Processing $_file..."</source>
 
Save and close the file. You can run it as follows:
 
Save and close the file. You can run it as follows:
<source lang="bash">chmod +x demp0.sh  
+
<syntaxhighlight lang="bash" >chmod +x demp0.sh  
 
./demp0.sh </source>
 
./demp0.sh </source>
 
Sample outputs:
 
Sample outputs:
 
<pre>Usage: ./demp0.sh filename</pre>
 
<pre>Usage: ./demp0.sh filename</pre>
 
Now, try to pass [[/etc/passwd]] filename :
 
Now, try to pass [[/etc/passwd]] filename :
<source lang="bash">./demp0.sh /etc/passwd</source>
+
<syntaxhighlight lang="bash" >./demp0.sh /etc/passwd</source>
 
Sample outputs:
 
Sample outputs:
 
<pre>./demp0.sh /etc/passwd
 
<pre>./demp0.sh /etc/passwd
Line 34: Line 34:
 
Processing /etc/passwd...</pre>
 
Processing /etc/passwd...</pre>
 
Finally, try to pass any /nonexistencefile,txt filename :
 
Finally, try to pass any /nonexistencefile,txt filename :
<source lang="bash">./demp0.sh /nonexistencefile,txt</source>
+
<syntaxhighlight lang="bash" >./demp0.sh /nonexistencefile,txt</source>
 
Sample outputs:
 
Sample outputs:
 
<pre>Script name: ./demp0.sh
 
<pre>Script name: ./demp0.sh

Revision as of 22:49, 29 March 2016

$0 is one of the Bash special parameters. It can only be referenced as follows (just an example as there are various ways to reference and use $0 while scripting) : <syntaxhighlight lang="bash" >echo "$0" echo "Usage: $0 fileName"</source> However, assignment to it is not allowed: <syntaxhighlight lang="bash" >0=foo</source>

Purpose

$0 expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. It is often used to display script usage message: <syntaxhighlight lang="bash" >#!/bin/bash _file="$1"

  1. if filename not supplied at the command prompt
  2. display usae message and die

[ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 1; }

echo "Script name: $0" echo "\$1 = $1, so \$_file set to $1"

  1. if file not found, display an error and die

[ ! -f "$_file" ] && { echo "$0: $_file file not found."; exit 2; }

  1. if we are here, means everything is okay

echo "Processing $_file..."</source> Save and close the file. You can run it as follows: <syntaxhighlight lang="bash" >chmod +x demp0.sh ./demp0.sh </source> Sample outputs:

Usage: ./demp0.sh filename

Now, try to pass /etc/passwd filename : <syntaxhighlight lang="bash" >./demp0.sh /etc/passwd</source> Sample outputs:

./demp0.sh /etc/passwd
Script name: ./demp0.sh
$1 = /etc/passwd, so $_file set to /etc/passwd
Processing /etc/passwd...

Finally, try to pass any /nonexistencefile,txt filename : <syntaxhighlight lang="bash" >./demp0.sh /nonexistencefile,txt</source> Sample outputs:

Script name: ./demp0.sh
$1 = /nonexistencefile,txt, so $_file set to /nonexistencefile,txt
./demp0.sh: /nonexistencefile,txt file not found.

If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero