Shell script arguments

  • Follow


In which variable are the arguments to a shell script?
E.g. if I run ./script --console-mode, where are '--console-mode'
stored? It is like the argv[] variable in C.
0
Reply hannibalkannibal (34) 8/1/2003 10:58:49 PM

On Fri, 01 Aug 2003 at 22:58 GMT, Eirik wrote:
> In which variable are the arguments to a shell script?
> E.g. if I run ./script --console-mode, where are '--console-mode'
> stored? It is like the argv[] variable in C.

   $1 $2 ....

man bash:

   Positional Parameters
       A positional parameter is a parameter denoted by one or more
       digits, other than the single digit 0.  Positional parameters
       are assigned from the shell's arguments when it is invoked, and
       may be reassigned using the set builtin command.  Positional
       parameters may not be assigned to with assignment statements.
       The positional parameters are temporarily replaced when a shell
       function is executed (see FUNCTIONS below).

       When a positional parameter consisting of more than a single
       digit is expanded, it must be enclosed in braces (see EXPANSION
       below).

-- 
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
0
Reply c.f.a.johnson (83) 8/2/2003 9:13:32 PM


No, in shell the command line arguments are stored in environment
variables called the "positional parameters".  To use them is
easy: $1 is the first word of the arguments, $2 is the second, etc.,
up to $9 (which holds all remaining args).

The shell will set some other args as well: "$*" is set to all
command line args, "$0" is set to the name of the script, and
"$#" is set to the number of command line args (useful with test
and "if" to see if any args are present).

The positional parameters can be reset using the set command:

	set -- These are new args
would set $# to 4, $1 to "These", etc.  (The "--" is not
required by a good habit.)

Hope this helps!  The shell is quite complex, I suggest you
find a good book or use an on-line guide.

-Wayne

Eirik wrote:
> In which variable are the arguments to a shell script?
> E.g. if I run ./script --console-mode, where are '--console-mode'
> stored? It is like the argv[] variable in C.

0
Reply Wayne 8/3/2003 2:36:26 AM

On Sat, 02 Aug 2003 22:36:26 -0400, Wayne <nospam@all.4me> wrote:
> 
> 
> No, in shell the command line arguments are stored in environment
> variables called the "positional parameters".  To use them is
> easy: $1 is the first word of the arguments, $2 is the second, etc.,
> up to $9 (which holds all remaining args).
> 

In bash2 they go on from there as ${10} ${11}, and so on.



> The shell will set some other args as well: "$*" is set to all
> command line args, "$0" is set to the name of the script, and
> "$#" is set to the number of command line args (useful with test
> and "if" to see if any args are present).
> 
> The positional parameters can be reset using the set command:
> 
> 	set -- These are new args
> would set $# to 4, $1 to "These", etc.  (The "--" is not
> required by a good habit.)
> 

Yeh. The -- unsets the positional parameters first.


Try set -- `ls` 



> Hope this helps!  The shell is quite complex, I suggest you
> find a good book or use an on-line guide.
> 


No way, for bash2, to beat the OREILLY book "Learning the bash Shell" 2nd
Edition (A few bucks on Amazon)

But google "shelldorado" and  find a nice tutorial to get started.


Alan




-- 
      For Linux/Bash users: Eliminate spam with the Mailbox-Sentry-Program. 
         See: http://tinyurl.com/inpd  for the scripts and docs.
     
0
Reply xxxxxx (166) 8/3/2003 3:13:50 AM

On Sun, 03 Aug 2003 at 02:36 GMT, Wayne wrote:
> 
> Eirik wrote:
>> In which variable are the arguments to a shell script?
>> E.g. if I run ./script --console-mode, where are '--console-mode'
>> stored? It is like the argv[] variable in C.

[please don't top post]

> No, in shell the command line arguments are stored in environment
> variables called the "positional parameters".  To use them is
> easy: $1 is the first word of the arguments, $2 is the second, etc.,
> up to $9 (which holds all remaining args).

    $9 doesn't hold the reamining parameters.

    In Bourne shells, one can only use $1 to $9, but if there are
    more, you can use shift to access higher-numbered parameters:

$ set a b c d e f g h i j k l m n o p q r s t u v w x y z
$ echo $9
i
$ echo $10
a0
$ echo ${10}
bad substitution
$ shift 9
$ echo $9
r

    In newer shells, there is no limit, but parameters greater than 9
    must be enclosed in braces:

$ set a b c d e f g h i j k l m n o p q r s t u v w x y z
$ echo $9
i
$ echo $10  ## echoes $1 followed by a literal 0
a0
$ echo ${10}
j

-- 
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
0
Reply c.f.a.johnson (83) 8/3/2003 9:56:30 AM

4 Replies
45 Views

(page loaded in 0.107 seconds)

Similiar Articles:













7/12/2012 3:21:43 PM


Reply: