[SOLVED] Use of cmd=`basename $0` in shell scripting
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello friends,
I have seen in many shell scripts, the variable used cmd=`basename $0`
What's use of this variable in a shell script? Is it necessory?
Though I've read one thread on the same, but that was not so informative.
So please give your expert opinions.
Thanks in advance!
That depends on the script ;-p You could use it for instance to find out what the script is called as. Like altering script execution by calling it via different named symbolic links.
Quote:
Originally Posted by meninvenus
Is it necessory?
If you need it, and there's no other or more efficient method, then it's necessary ;-p
From what it looks like, basename is a way to get the name of a file without an extension and the $0 variable is the name of the script being run.
This would mean that if you're running a script called script.sh, when cmd is called, it will just be equal to script.
Not really true. basename removes the leading path and leaves the filename intact. For example
Code:
/directory/somewhere/in/my/system/script.sh
becomes
Code:
script.sh
Regarding the suffix, it is an additional feature: if you specify a second argument to basename it will be treated as a suffix to remove, so that in the previous example the command
Not really true. basename removes the leading path and leaves the filename intact. For example
Code:
/directory/somewhere/in/my/system/script.sh
becomes
Code:
script.sh
Regarding the suffix, it is an additional feature: if you specify a second argument to basename it will be treated as a suffix to remove, so that in the previous example the command
Thanks for clearing that up, that explains why most of where I've seen basename used is in init scripts. They don't have file extensions and it may be necessary to call back to what service is being started/stopped.
One use that I often use it for is to provide usage information, and you don't know if someone has retitled your script. You can also use it to remove a suffix, but that is not its main feature.
As long as you use Bash you are right, no need for basename. But many scripts are written POSIX compliant, which doesn't nknow of that substitution features of Bash (and other shells). So if you aim at portability basename is the preferred solution.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.