[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] View Positional Parameters of Commands
From: |
Michael Convey |
Subject: |
Re: [Help-bash] View Positional Parameters of Commands |
Date: |
Tue, 28 Jul 2015 17:50:35 -0700 |
On Tue, Jul 28, 2015 at 8:11 AM, Greg Wooledge <address@hidden> wrote:
> No, that's not correct. In this example, there is only one positional
> parameter, "-al".
>
> I see, thanks.
> Let's say I write a script, like this:
>
> ===========================================
> #!/bin/sh
>
> 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.
>
> I see -- just like Clark Wang' s excellent function example. I ran a
similar function on my machine as follows:
$ foo() { echo $0 $1 $2 $3 $4; }
$ declare -F
declare -f foo
$ foo -a
bash -a
$ foo -a -b -c
bash -a -b -c
$
Why is the output of $0 'bash' instead of 'foo'?
> 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.
>
> $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.
>
>
I see, so this only works with scripts or functions arguments and not
binary commands arguments. I misunderstood.
> Of your CHILDREN? You know what they are because you just wrote them.
>
Yes, I understand that, but I just learned about positional parameters and
I was trying to confirm my understanding of how they work and in what
contexts. From your and Clark's feedback, I believe they only work for
scripts and functions, but not binary commands, right? I believe a few
Linux commands are scripts instead of binary programs -- so positional
parameters are used by those commands, right?
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).
>
I confess your preceding sentence went over my head, but I'll get there
eventually. =)
>
> The arguments that YOU passed TO a process IN THE PAST are not saved
>
> by bash.
>
Then why does this work?
$ foo() { echo $0 $1 $2 $3 $4; }
$ foo -a
bash -a
>
> $1 etc. are for the arguments that you RECEIVED from your caller.
>
Do you mean "the arguments that the script received from the user"?