[Top][All Lists]

[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: Tue, 28 Jul 2015 11:11:48 -0400
User-agent: Mutt/

On Tue, Jul 28, 2015 at 07:58:08AM -0700, Michael Convey wrote:
> I'm trying to understand positional parameters. If I run the the following
> command:
> ls -al
> echo $0 should equal ls
> echo $1 should equal -a
> echo $2 should equal -l

No, that's not correct.  In this example, there is only one positional
parameter, "-al".

> However, no matter which command I run, echo $0 always equals "bash" and
> $1, $2, etc. are empty.

You're not describing the setup very well.

Let's say I write a script, like this:


echo "\$0 is <$0>"
echo "\$1 is <$1>"
echo "\$2 is <$2>"

If I run that with various arguments, I will see them printed inside
angle brackets.

If you are running a command like "ls -al" but you didn't write a script
or function named "ls", or didn't put your custom ls script in PATH
before the system's ls(1), then you are not running your script.  You
are running the system's ls(1) command.

ls is usually written in C, so it would access its arguments via
the argv[] parameter of the main() function.  None of that is exposed
to the program that called it.

> bash: source: /usr/bin/command: cannot execute binary file

What on EARTH are you trying to do?

> So I tried the following:
> ls -al; echo $0; echo $1

What on EARTH did you expect this to do?

$0 and $1 are your SCRIPT'S arguments, received from the program that
ran your script.  They have nothing to do with the ls -al command
that your script ran just a moment ago.

> But, I get the same results. What is the best way to do what I'm trying to
> accomplish -- view positional parameters of commands I run?

Of your CHILDREN?  You know what they are because you just wrote them.
You invoked ls with argv[] = { "ls", "-al", NULL } (if I may be allowed
to invent a syntax to represent an array of char * in memory).

The arguments that YOU passed TO a process IN THE PAST are not saved
by bash.

$1 etc. are for the arguments that you RECEIVED from your caller.

reply via email to

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