help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] View Positional Parameters of Commands


From: Greg Wooledge
Subject: Re: [Help-bash] View Positional Parameters of Commands
Date: Wed, 29 Jul 2015 08:14:47 -0400
User-agent: Mutt/1.4.2.3i

On Tue, Jul 28, 2015 at 05:50:35PM -0700, Michael Convey wrote:
> ???I see, so this only works with scripts or functions arguments and not
> binary commands arguments. ???I misunderstood.
> ???

You're still missing a fundamental piece.  I'm not sure how to explain it,
since your misunderstanding is so deep.

Let's say you have a script, "./foo".

Someone types "./foo -a bar".  This causes the kernel to execute your
script.  Since your script begins with the magic characters #! the
kernel invokes an interpreter and passes your script as an argument,
and also passes "-a" and "bar" as additional arguments.

So, within your script, "-a" and "bar" are available as positional
parameters.  This is what "$1" and "$2" and so on give you.

$0 gives you GARBAGE and is not useful.  Just ignore it.

Now suppose your script runs the command "ls -l foobar".  Your script
is a process, and it has its own arguments ("-a" and "bar").  The
kernel launches a NEW process (/bin/ls or whatever it is on your system),
and passes "-l" and "foobar" as arguments to that NEW process.  So
you have two processes running under this user ID: "./foo" with
arguments "-a" and "bar", and "ls" with arguments "-l" and "foobar".

The values of $1, $2 and so on within your script DO NOT CHANGE just
because you launched a child process.  That child process is totally
independent.  It has its own arguments, its own copy of the environment,
its own private variables, its own private file descriptors for reading
and writing, etc.  Nothing that it does affects your script.

After ls terminates, the values of $1 and $2 within your script are
still "-a" and "bar" respectively.

Now, *what are you trying to do*?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]