LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-19-2012, 10:48 PM   #1
johnifanx98
LQ Newbie
 
Registered: Jan 2008
Posts: 26

Rep: Reputation: 0
Why $0 shows as "-bash", not the script name?


test(){
echo $0
echo $1
echo $2
}

run "test 1 2", and it gives

-bash
1
2

I expect

test
1
2
 
Old 02-19-2012, 11:06 PM   #2
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 36
Quote:
Originally Posted by johnifanx98 View Post
test(){
echo $0
echo $1
echo $2
}

run "test 1 2", and it gives

-bash
1
2

I expect

test
1
2
$0 is supposed to print the name of the script.
Here is the output when i run it.
Code:
test
1
2
Check the script.
Code:
test()
{
echo $0
echo $1
echo $2
}
test 1 2
Are you supplying command line arguments?
 
Old 02-20-2012, 01:21 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
How are you calling it
Code:
bash yourscript

# OR

./yourscript
 
Old 02-20-2012, 01:30 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 806

Rep: Reputation: 186Reputation: 186
Actually the op is saying run "test 1 2".

Your $0 is run (under bash) and "test" is "shift"ed out.

OK
 
Old 02-20-2012, 04:08 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by AnanthaP View Post
Actually the op is saying run "test 1 2"
Code:
c@CW8:~$ type run
bash: type: run: not found
 
Old 02-20-2012, 04:32 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
I think the most robust method is by using the appropriate bash variables. This avoid confusion between executed and sourced scripts. There is also a specific variable to retrieve function names:
Code:
#!/bin/bash
#
test () {
  echo $FUNCNAME
  echo $1
  echo $2
}

test 1 2

echo $(readlink -f "$BASH_SOURCE")
The last statement gives the full path of the script:
Code:
$ ./test.sh
test
1
2
/home/colucix/test.sh
$ . test.sh
test
1
2
/home/colucix/test.sh
$ bash test.sh
test
1
2
/home/colucix/test.sh
Hope this helps.
 
Old 02-20-2012, 11:22 AM   #7
johnifanx98
LQ Newbie
 
Registered: Jan 2008
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
I think the most robust method is by using the appropriate bash variables. This avoid confusion between executed and sourced scripts. There is also a specific variable to retrieve function names:
Code:
#!/bin/bash
#
test () {
  echo $FUNCNAME
  echo $1
  echo $2
}

test 1 2

echo $(readlink -f "$BASH_SOURCE")
The last statement gives the full path of the script:
Code:
$ ./test.sh
test
1
2
/home/colucix/test.sh
$ . test.sh
test
1
2
/home/colucix/test.sh
$ bash test.sh
test
1
2
/home/colucix/test.sh
Hope this helps.
Thanks a lot for all responses. Now I see I'm running a function not script which caused this unexpected result...
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
Strangely, bash reports "no such file", but it shows with ls. ramchip Slackware 2 02-25-2008 01:03 PM
Xfce terminal shows "bash-3.11" instead of the path jsmith6 Slackware 5 01-13-2008 03:30 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 10:00 AM


All times are GMT -5. The time now is 05:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration