Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to get script name, script path within the bash script?


Asked by Charles Melton on Dec 04, 2021 FAQ



Now in this article I will share some tips to get the script name in shell script, get script path in bash script with examples. You can collect these values from within the script. Use the below variable within the script to get the script name from within the shell script So both our variables are giving us the script name of the executed script.
Moreover,
Get script name in shell script. Use the below variable within the script to get the script name from within the shell script. #!/bin/bash script_name1=`basename $0` script_name2=`basename "$(realpath $0)"` echo $script_name1 echo $script_name2. Let us execute the script.
Besides, Alternatively, you can modify the PATH environment variable in your .bashrc file and use the “source” command in order to refresh your current Bash environment. Exit the file and source your bashrc file for the changes to be applied. Great! Now your script can be executed from where you want on your system.
Also Know,
In order to run a Bash script from anywhere on your system, you need to add your script to your PATH environment variable. $ export PATH="<path_to_script>:$PATH" Now that the path to the script is added to PATH, you can call it from where you want on your system.
Keeping this in consideration,
I only need to support Bash, not sh, csh, etc. What I've found so far: The accepted answer to Getting the source directory of a Bash script from withinaddresses getting the path of the script via dirname $0, which is fine, but that may return a relativepath (like .