LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 06-15-2015, 08:46 AM   #1
starqazi
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Rep: Reputation: Disabled
Unable to get script name as output


Hi All,

I m trying to get script name of executed script.
The command in the script is:

prog1=$(echo $0 | sed 's/.*\///g;s/\.sh//g')
echo $prog1



It is only returning "bash".

What is wrong with this command
 
Old 06-15-2015, 10:02 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It works for me, assuming your intention is to strip off any leading directory names and any .sh extension.

It would be safer to put double quotes around the $0

Using $0 is not robust though, as detailed at http://mywiki.wooledge.org/BashFAQ/028

How did you test it? Meaning what command did you use. And what was the result?
 
Old 06-15-2015, 11:13 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Works for me too.
Code:
$ echo $0 | sed 's/.*\///g;s/\.sh//g'
bash
$ cat script.sh
#!/bin/bash

echo $0 | sed 's/.*\///g;s/\.sh//g'
$ ./script.sh
script
 
Old 06-15-2015, 12:35 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
vi LQRocks.sh
and add
Code:
#!/bin/bash
echo `basename $0`
gives you the script name when run from a script. NOT a shell.
Code:
./LQRocks.sh
LQRocks.sh

Change the script name...reflects in the script:
Code:
mv LQRocks.sh YesItDoes.sh
Code:
./YesItDoes.sh
YesItDoes.sh

Enjoy the Goodness.

Last edited by Habitual; 06-15-2015 at 12:38 PM.
 
Old 06-16-2015, 02:13 AM   #5
starqazi
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thank All who replied my post. I found your posts really help and with your help I resolved the issue.

Thanks & Regards,
Qazi
 
Old 06-16-2015, 04:14 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
echo ${0##*/}
 
Old 06-16-2015, 11:32 AM   #7
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
test.sh
Code:
prog1=$(echo $0 | sed 's/.*\///g;s/\.sh//g')
echo $prog1
If you run
./test.sh
test

the response was test
If however, you source the file, the response is bash
Code:
.  ./test.sh
bash
With sourcing, the result of $0 is the name of the calling script

in place of echo and sed

test.sh
Code:
prog1=$(basename $0 .sh)    
echo "$prog1 is my script name"
Please note: $0 changes when the script is sourced.
If you are going to source your script, you would need test.sh as an argument to another script.

test2.sh
Code:
#!/bin/bash
prog1=$(basename $1 .sh )
Now ./test2.sh test.sh
yields test
and
. ./test2.sh test.sh
yields test.

Why do we want to do this anyway. Well here is a situation
Suppose I have a group of directories titled project1, project2, project3, ...
and my "project1.sh", "project2.sh", "project3.sh" ... script have

Code:
#!/bin/bash
prog1=$(basename $0 .sh)
cd $prog1
ls -l | grep -v total
#
If I do project1.sh
I will see the contents of project1

If I want to also switch to project1
I would just like to do
. project1.sh

to switch to that directory and after the listing, to become my current directory.
 
  


Reply



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
How do I redirect output from bash script WITHIN script Robert S Linux - General 7 07-19-2013 10:42 AM
[SOLVED] How to find out the output of a script is success or not in another script jhonnappier2007 Linux - Newbie 6 11-10-2011 12:37 AM
Standard script output to screen & -x option output to file BoBeau236 Programming 6 05-10-2011 07:43 AM
Unable to redirect script output to a file alien12 Linux - Newbie 4 08-25-2009 03:36 AM
Correct output from script appears only when script is run interactively kaplan71 Linux - Software 2 01-15-2009 11:47 AM

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

All times are GMT -5. The time now is 04:43 AM.

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