[Linux-Discussion] Re: what does the variable $1 mean in Bourneshell?

  • From: "Brian L. Johnson" <blj8@xxxxxxxx>
  • To: linux-discussion@xxxxxxxxxxxxx
  • Date: Tue, 18 Dec 2001 16:38:18 -0700 (MST)

On Dec 18, David Bruce Jr did the finger mambo upon the keyboard:

>$1 means "assign this value to a variable, so later on in the shell script
>all you
>gotta do is use $1 and you don't have to write the whole file name"
>I think

Well, yes and no... $1, $2, etc are variables, but are special ones used
to represent the commandline parameters. So if you run a script like
this:

scriptfile.sh bat box fruit ball car

$1 would be "bat"
$2 would be "box"
etc...

As for "$?":

(From the man page for Bash)

       *      Expands to the positional parameters, starting from
              one.   When  the  expansion  occurs  within  double
              quotes,  it expands to a single word with the value
              of each parameter separated by the first  character
              of  the  IFS  special variable.  That is, ``$*'' is
              equivalent to ``$1c$2c...'', where c is  the  first
              character of the value of the IFS variable.  If IFS
              is null or unset, the parameters are  separated  by
              spaces.
       @      Expands to the positional parameters, starting from
              one.   When  the  expansion  occurs  within  double
              quotes,  each parameter expands as a separate word.
              That is, `` $@'' is equivalent to ``$1'' ``$2'' ...
              When there are no positional parameters, ``$@'' and
              $@ expand to nothing (i.e., they are removed).
       #      Expands to the number of positional  parameters  in
              decimal.
       ?      Expands to the status of the most recently executed
              foreground pipeline.
       -      Expands to the current option  flags  as  specified
              upon  invocation,  by  the  set builtin command, or
              those set by the  shell  itself  (such  as  the  -i
              flag).
       $      Expands  to  the  process ID of the shell.  In a ()
              subshell, it expands to the process ID of the  cur=AD
              rent shell, not the subshell.
       !      Expands to the process ID of the most recently exe=AD
              cuted background (asynchronous) command.
       0      Expands to the name of the shell or  shell  script.
              This  is  set  at shell initialization.  If bash is
              invoked with a file of commands, $0 is set  to  the
              name  of that file.  If bash is started with the -c
              option, then $0 is set to the first argument  after
              the string to be executed, if one is present.  Oth=AD
              erwise, it is set to the pathname  used  to  invoke
              bash, as given by argument zero.
       _      Expands  to  the last argument to the previous com=AD
              mand, after expansion.  Also set to the full  path=AD
              name  of  each  command  executed and placed in the
              environment exported to that command.

  -=3DBrian L. Johnson,  www.blj8.com=3D-
        9136 links at blj8.com!
   ---------------------------------
 For PGP key email to: pgpkey@xxxxxxxx
For Geekcode email to: geekcode@xxxxxxxx



Other related posts: