$0 Script File Name Parameter Usage In Linux Bash

Bash provides different parameters to get input from different sources. $0 is a special parameter that is used to store the name of the bash script. This $0 parameter is automatically assigned by the bash or bash interpreter and can not be changed in a script file like a regular variable. This makes it a special parameter managed by the bash. The $0 parameter can be only read.

$0 File Name Parameter Example

As the $0 special parameter is assigned with the current script file name automatically there is no need to initialize the script.

#!/bin/bash

print "This bash script file name is  $0"

print "The first parameter to this script file is  $1"

When we execute this scripting file like below the output first line will provides the current script file name which is provided by the special parameter $0. The second line provides the first parameter provided this script file which is stored inside the $1 parameter.

$ ./myscript.sh 123
This bash script file name is ./myscript.sh
The first parameter to this script file is 123

Can I Set $0 Script File Name Parameter?

You may ask that the $0 is like a variable and can I set the $0 script file name parameter? You can not set this special parameter because it is read-only and if we try to set a new value for this parameter we will get an error like “/bin/bash: warning: shell level (1000) too high, resetting to 1” and “./myscript.sh: fork: retry: Resource temporarily unavailable” and as the last, after some time the following error will be printed to the terminal output “This bash script file name is ./myscript.sh
The first parameter to this script file is =

#!/bin/bash

$0 = "Thisisnewfilename.sh"

echo "This bash script file name is  $0"

echo "The first parameter to this script file is  $1"

Let’s execute the error prone bash script like below.

$ ./myscript.sh 123

The output will be like below.

/bin/bash: warning: shell level (1000) too high, resetting to 1
/bin/bash: warning: shell level (1000) too high, resetting to 1
/bin/bash: warning: shell level (1000) too high, resetting to 1
/bin/bash: warning: shell level (1000) too high, resetting to 1
/bin/bash: warning: shell level (1000) too high, resetting to 1
/bin/bash: warning: shell level (1000) too high, resetting to 1
/bin/bash: warning: shell level (1000) too high, resetting to 1
/bin/bash: warning: shell level (1000) too high, resetting to 1
/bin/bash: warning: shell level (1000) too high, resetting to 1
./myscript.sh: fork: retry: Resource temporarily unavailable
./myscript.sh: fork: retry: Resource temporarily unavailable
./myscript.sh: fork: retry: Resource temporarily unavailable
./myscript.sh: fork: retry: Resource temporarily unavailable
./myscript.sh: fork: Resource temporarily unavailable
This bash script file name is ./myscript.sh
The first parameter to this script file is =
This bash script file name is ./myscript.sh
The first parameter to this script file is =
This bash script file name is ./myscript.sh
The first parameter to this script file is =
This bash script file name is ./myscript.sh
READ  Print Line Number For Grep Command

Leave a Comment