Linux - General This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-05-2012, 09:03 PM
|
#1
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,676
|
Use of cmd=`basename $0` in shell scripting
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!
|
|
|
|
10-05-2012, 09:41 PM
|
#2
|
|
Moderator
Registered: May 2001
Posts: 24,969
|
Quote:
Originally Posted by meninvenus
What's use of this variable in a shell script?
|
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
|
|
|
|
10-05-2012, 10:26 PM
|
#3
|
|
Member
Registered: Oct 2011
Distribution: CentOS 5.6
Posts: 38
Rep:
|
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.
|
|
|
|
10-06-2012, 03:29 AM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Quote:
Originally Posted by Toggan
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
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
Code:
basename /directory/somewhere/in/my/system/script.sh .sh
leaves only script. 
|
|
|
1 members found this post helpful.
|
10-06-2012, 10:29 AM
|
#5
|
|
Member
Registered: Oct 2011
Distribution: CentOS 5.6
Posts: 38
Rep:
|
Quote:
Originally Posted by colucix
Not really true. basename removes the leading path and leaves the filename intact. For example
Code:
/directory/somewhere/in/my/system/script.sh
becomes
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
Code:
basename /directory/somewhere/in/my/system/script.sh .sh
leaves only script. 
|
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.
|
|
|
|
10-06-2012, 11:12 AM
|
#6
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,707
|
Read 'man basename' for all of its features.
Do not use backticks use this instead:
Code:
cmd="$(basename $0)"
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.
|
|
|
1 members found this post helpful.
|
10-06-2012, 12:01 PM
|
#7
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,882
|
pretty strange, noone mentioned, basename is an external utility, and this can be solved inside bash.
Code:
X=$0 # save $0 in a local variable
Y=${X##*/} # remove dir part
Z=${Y%.*} # remove extension - if you want to do so
http://splike.com/wiki/Bash_Scriptin...unctions.29.3F - there is a typo on that page, so probably this one is better: http://linuxgazette.net/18/bash.html
|
|
|
1 members found this post helpful.
|
10-06-2012, 12:48 PM
|
#8
|
|
Moderator
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Slackware, Debian
Posts: 12,524
|
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.
|
|
|
1 members found this post helpful.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:28 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|