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: pass arguments in bash - comp.unix.programmerimporting functions from an external bash script - comp.unix.shell ... He wants to be able to pass an argument to an external ... How to pass arguments to a Bash-script ... execute shell script from python, needs sys.argv - comp.lang ...> But, this does not seem to work as this is not the proper way to pass > arguments to the shell script. > > in short, I'm sure that this is a easy fix, but given my ... Capturing Stored Procedure Result inside Shell Script - comp.unix ...... need to capture the output of a Sybase stored procedure, inside my shell script( k shell). Based on this output, I need to call another perl script, with input arguments ... Pass command line parameters when running an application from a ...Pass command line parameters when running an application from a ... execute shell script from python, needs sys.argv - comp.lang ... fastx.sh, and pass it's arguments to ... How pass a newline character as an argument to a command? (in bash ...What I want is to pass an newline character as an argument to any program. ... Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) Pro Bash ... Bash script - telnet - comp.unix.shellOS: solaris shell: bash I have a UNIX shell script which appends new text to a file. ... shell prompt, what does ... printer aix telnet printer script telnet script ... Safe script to kill processes - comp.unix.solarisWe rlogin to A from B, start applications for B with "-display B:0.0" argument. Now I'd like a safe, conservative shell script to rlogin to A and kill those processes ... importing functions from an external bash script - comp.unix.shell ...pass arguments in bash - comp.unix.programmer importing functions from an external bash script - comp.unix.shell ... pass arguments in bash - comp.unix.programmer Hello ... AWK command inside the shell script - comp.lang.awkAWK command inside the shell script - comp.lang.awk awk script to split text ... Another way is to test the command-line parameters in the shell script before invoking awk. how to simulate press "spacebar, enter, esc keys" at the unix ...Test regex in KSH - comp.unix.shell (sh/bash ... how to simulate press "spacebar, enter, esc keys" at the ... ... Post: unix if arguments exist shell script pass string ... Passing arguments to a shell script - DocView: Access to SCO ...Passing arguments to a shell script. Any shell script you run has access to (inherits) the environment variables accessible to its parent shell. UNIX/Linux Bash Shell Scripting: How to Pass Arguments to Shell ScriptLike UNIX commands, shell scripts also accept arguments from the command line. They can, therefore, run non interactively and be used with redirection and 7/12/2012 3:21:43 PM
|