LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 09: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, 10:06 PM   #2
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
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, 12:21 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

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

# OR

./yourscript
 
Old 02-20-2012, 12:30 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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, 03:08 AM   #5
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
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, 03:32 AM   #6
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
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, 10: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



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 01:03 AM
Strangely, bash reports "no such file", but it shows with ls. ramchip Slackware 2 02-25-2008 12:03 PM
Xfce terminal shows "bash-3.11" instead of the path jsmith6 Slackware 5 01-13-2008 02:30 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:39 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