LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-29-2019, 03:06 PM   #1
atharvdesai1996
LQ Newbie
 
Registered: Dec 2019
Posts: 15

Rep: Reputation: Disabled
Meaning of $0, $1 in shell


What is the meaning of $0, $1 etc in shell?

When I type $0 in ubuntu terminal, I get written as bash.
When I type $1 , $2etc and hit Enter, nothing appears on next line.

Please Explain
 
Old 12-29-2019, 03:12 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
#!/usr/bin/env bash

##enter four separate words on the command line then hit enter

echo $0 $1 $2 $3 $4
make that into a script, then run it.
./script .....

Last edited by BW-userx; 12-29-2019 at 03:14 PM.
 
2 members found this post helpful.
Old 12-29-2019, 03:32 PM   #3
atharvdesai1996
LQ Newbie
 
Registered: Dec 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
Smile Thanks alott!!

Quote:
Originally Posted by BW-userx View Post
Code:
#!/usr/bin/env bash

##enter four separate words on the command line then hit enter

echo $0 $1 $2 $3 $4
make that into a script, then run it.
./script .....
Thanks alot.
 
1 members found this post helpful.
Old 12-29-2019, 05:49 PM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,597

Rep: Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545
$1 and above are positional parameters which relate to a script's arguments - the Bash documentation for which is at https://www.gnu.org/software/bash/ma...nal-Parameters, and the POSIX specification is at https://pubs.opengroup.org/onlinepub...l#tag_18_05_01

$0 is a Special Parameter which expands to the name of the shell or script being run.
Another special parameter is $# which is the number of positional parameters.
 
Old 12-29-2019, 06:05 PM   #5
atharvdesai1996
LQ Newbie
 
Registered: Dec 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
Smile Thanks!!!!!

Quote:
Originally Posted by boughtonp View Post
$1 and above are positional parameters which relate to a script's arguments - the Bash documentation for which is at https://www.gnu.org/software/bash/ma...nal-Parameters, and the POSIX specification is at https://pubs.opengroup.org/onlinepub...l#tag_18_05_01

$0 is a Special Parameter which expands to the name of the shell or script being run.
Another special parameter is $# which is the number of positional parameters.
Thanks!!!
 
Old 12-29-2019, 08:44 PM   #6
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,597

Rep: Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691
BTW: there is a lovely MAN page for BASH that should be installed on your system. I go back and read it about once a year, to rediscover useful things I might otherwise forget.
 
  


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
What is the meaning of $# in shell scripts? ravi_nandula Linux - Newbie 17 03-28-2012 06:00 AM
[SOLVED] what is the meaning $$ in shell procfs Linux - Newbie 3 01-13-2011 11:25 PM
meaning of if [ -z $SOMETHING....] in bash shell darwinianlo Linux - Newbie 8 08-09-2007 11:02 AM
"^" meaning in perl/shell script? fhleung Programming 8 01-03-2006 07:27 PM
'./file' meaning in python & shell script Chowroc Programming 7 12-30-2005 02:32 AM

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

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