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! |
Quote:
Quote:
|
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. |
Quote:
Code:
/directory/somewhere/in/my/system/script.sh Code:
script.sh Code:
basename /directory/somewhere/in/my/system/script.sh .sh |
Quote:
|
Read 'man basename' for all of its features.
Do not use backticks use this instead: Code:
cmd="$(basename $0)" |
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 |
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.
|
All times are GMT -5. The time now is 07:45 AM. |