LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Meaning of $0, $1 in shell (https://www.linuxquestions.org/questions/linux-newbie-8/meaning-of-%240-%241-in-shell-4175666727/)

atharvdesai1996 12-29-2019 03:06 PM

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

BW-userx 12-29-2019 03:12 PM

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 .....

atharvdesai1996 12-29-2019 03:32 PM

Thanks alott!!
 
Quote:

Originally Posted by BW-userx (Post 6072354)
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.

boughtonp 12-29-2019 05:49 PM

$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.

atharvdesai1996 12-29-2019 06:05 PM

Thanks!!!!!
 
Quote:

Originally Posted by boughtonp (Post 6072399)
$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!!!

wpeckham 12-29-2019 08:44 PM

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.


All times are GMT -5. The time now is 07:30 PM.