LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 10-05-2012, 09:03 PM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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!
 
Old 10-05-2012, 09:41 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599
Quote:
Originally Posted by meninvenus View Post
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 View Post
Is it necessory?
If you need it, and there's no other or more efficient method, then it's necessary ;-p
 
Old 10-05-2012, 10:26 PM   #3
Toggan
Member
 
Registered: Oct 2011
Distribution: CentOS 5.9
Posts: 39

Rep: Reputation: 6
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.
 
Old 10-06-2012, 03:29 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Toggan View Post
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
Code:
basename /directory/somewhere/in/my/system/script.sh .sh
leaves only script.
 
1 members found this post helpful.
Old 10-06-2012, 10:29 AM   #5
Toggan
Member
 
Registered: Oct 2011
Distribution: CentOS 5.9
Posts: 39

Rep: Reputation: 6
Quote:
Originally Posted by colucix View Post
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
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.
 
Old 10-06-2012, 11:12 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1300Reputation: 1300Reputation: 1300Reputation: 1300Reputation: 1300Reputation: 1300Reputation: 1300Reputation: 1300Reputation: 1300Reputation: 1300
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.
Old 10-06-2012, 12:01 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,844

Rep: Reputation: 6704Reputation: 6704Reputation: 6704Reputation: 6704Reputation: 6704Reputation: 6704Reputation: 6704Reputation: 6704Reputation: 6704Reputation: 6704Reputation: 6704
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.
Old 10-06-2012, 12:48 PM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4885Reputation: 4885Reputation: 4885Reputation: 4885Reputation: 4885Reputation: 4885Reputation: 4885Reputation: 4885Reputation: 4885Reputation: 4885Reputation: 4885
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.
  


Reply

Tags
shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue sg_modes cmd at cmd line, want to see the cmd in binary form NuUser Linux - Newbie 1 03-28-2012 08:08 AM
“basename $0” returns basename: -k: unknown option jaideepca Linux - Newbie 6 11-09-2009 05:30 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 01:41 AM
Awk and Shell CMD Output xanthium Programming 16 04-24-2002 06:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration