Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

print the number of command line args

3 views
Skip to first unread message

Harry Putnam

unread,
Jan 26, 2012, 5:19:41 PM1/26/12
to
I'm having a time here just trying to test/print the number of command
line arguments.

Running: GNU bash, version 4.2.20(1)-release (i486-pc-linux-gnu)

I know that value can be accessed in the same way as any array but am
not getting the syntax right. Googling has produced a large pile of
hits and many things about showing the count of array elements and
about `$@' the command line array. But again not finding the exact
syntax.

bash script `scr.bash'
------- 8< snip ---------- 8< snip ---------- 8<snip -------
#!/usr/local/bin/bash

array=( one two three)

echo ' This array:
array=( one two three)
has: < '${#array[@]} " > elements
------- ------- ---=--- ------- -------
"

echo "my command line has < ${#[@]} > elements"

echo "------- ------- ---=--- ------- -------
"
echo "My command line is <$*>"
------- --------- ---=--- --------- --------
Call it like: `scr now what':
Output:
,----
| This array:
| array=( one two three)
| has: < 3 > elements
| ------- ------- ---=--- ------- -------
|
| ./scr: line 11: my command line has < ${#[@]} > elements: bad substitution
| ------- ------- ---=--- ------- -------
|
| My command line is <now what>
`----
NOT
------- --------- ---=--- --------- --------
Changed to: ${#$[@]}

(same output)

NOT
------- --------- ---=--- --------- --------

Changed to ${$#[@]}
Output:

,----
| This array:
| array=( one two three)
| has: < 3 > elements
| ------- ------- ---=--- ------- -------
|
| my command line has < 11171 > elements
| ------- ------- ---=--- ------- -------
|
| My command line is <now what>
`----

What does that number (11171) mean?
NOT
------- --------- ---=--- --------- --------

Finally in desperation I changed it to a simple `$#'
Output:

,----
| This array:
| array=( one two three)
| has: < 3 > elements
| ------- ------- ---=--- ------- -------
|
| my command line has < 2 > elements
| ------- ------- ---=--- ------- -------
|
| My command line is <now what>
`----
YIPPEE
Egad, that appears to be what I'm after. But is that really the way?





Sven Mascheck

unread,
Jan 26, 2012, 5:49:25 PM1/26/12
to
Harry Putnam wrote:

> I'm having a time here just trying to test/print the number of command
> line arguments.
>
> Running: GNU bash, version 4.2.20(1)-release (i486-pc-linux-gnu)
>
> I know that value can be accessed in the same way as any array [...]

"command line arguments" aka "positional parameters" are not
connected to an array, though.

$@ and $* may look alike, but they're just special parameters.
And thus '$#' is the correct way to get the number of arguments.

If you feel like using an array anyway, you could do array=($@)

Thomas 'PointedEars' Lahn

unread,
Jan 26, 2012, 7:23:40 PM1/26/12
to
Harry Putnam wrote:

> I'm having a time here just trying to test/print the number of command
> line arguments.

A *hard* time, I suppose.

> Running: GNU bash, version 4.2.20(1)-release (i486-pc-linux-gnu)
>
> I know that value can be accessed in the same way as any array

You are mistaken.

> but am not getting the syntax right. Googling has produced a large pile
> of hits and many things about showing the count of array elements and
> about `$@' the command line array.

It is not an array.

> But again not finding the exact syntax.
> […]
> Finally in desperation I changed it to a simple `$#'

You should have posted the real code.

> Output:
>
> ,----
> | This array:
> | array=( one two three)
> | has: < 3 > elements
> | ------- ------- ---=--- ------- -------
> |
> | my command line has < 2 > elements
> | ------- ------- ---=--- ------- -------
> |
> | My command line is <now what>
> `----
> YIPPEE
> Egad, that appears to be what I'm after. But is that really the way?

The `$#' prefix is what you must use for the number of command-line
arguments. I do not understand what your array has to do with this.
So RTFM, not (only) Google results.

man bash | less -p 'Parameter Expansion'

--
PointedEars

Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Chris F.A. Johnson

unread,
Jan 26, 2012, 6:10:28 PM1/26/12
to
That is the same as array=( $* ); it should be: array=( "$@" )

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

Chris F.A. Johnson

unread,
Jan 26, 2012, 6:09:07 PM1/26/12
to
On 2012-01-26, Harry Putnam wrote:
> I'm having a time here just trying to test/print the number of command
> line arguments.
>
> Running: GNU bash, version 4.2.20(1)-release (i486-pc-linux-gnu)
>
> I know that value can be accessed in the same way as any array but am
> not getting the syntax right. Googling has produced a large pile of
> hits and many things about showing the count of array elements and
> about `$@' the command line array. But again not finding the exact
> syntax.
>
> bash script `scr.bash'
> ------- 8< snip ---------- 8< snip ---------- 8<snip -------
> #!/usr/local/bin/bash
>
> array=( one two three)
>
> echo ' This array:
> array=( one two three)
> has: < '${#array[@]} " > elements
> ------- ------- ---=--- ------- -------
> "
>
> echo "my command line has < ${#[@]} > elements"

The number of arguments is $#

> echo "------- ------- ---=--- ------- -------
> "
> echo "My command line is <$*>"

> ------- --------- ---=--- --------- --------
> Call it like: `scr now what':
> Output:
> ,----
>| This array:
>| array=( one two three)
>| has: < 3 > elements
>| ------- ------- ---=--- ------- -------
>|
>| ./scr: line 11: my command line has < ${#[@]} > elements: bad substitution

$# is not an array variable.

>| ------- ------- ---=--- ------- -------
>|
>| My command line is <now what>
> `----
> NOT
> ------- --------- ---=--- --------- --------
> Changed to: ${#$[@]}
>
> (same output)
>
> NOT
> ------- --------- ---=--- --------- --------
>
> Changed to ${$#[@]}

That is the same as ${$}; # tells it to remove a leading @ (if
there is one).

> Output:
>
> ,----
>| This array:
>| array=( one two three)
>| has: < 3 > elements
>| ------- ------- ---=--- ------- -------
>|
>| my command line has < 11171 > elements
>| ------- ------- ---=--- ------- -------
>|
>| My command line is <now what>
> `----
>
> What does that number (11171) mean?

Try: echo $$

> NOT
> ------- --------- ---=--- --------- --------
>
> Finally in desperation I changed it to a simple `$#'

As it should be!

> Output:
>
> ,----
>| This array:
>| array=( one two three)
>| has: < 3 > elements
>| ------- ------- ---=--- ------- -------
>|
>| my command line has < 2 > elements
>| ------- ------- ---=--- ------- -------
>|
>| My command line is <now what>
> `----
> YIPPEE
> Egad, that appears to be what I'm after. But is that really the way?

Yes. $# is the number of arguments.

Stephane Chazelas

unread,
Jan 27, 2012, 6:17:58 AM1/27/12
to
2012-01-26 18:10:28 -0500, Chris F.A. Johnson:
[...]
> > If you feel like using an array anyway, you could do array=($@)
>
> That is the same as array=( $* ); it should be: array=( "$@" )
[...]

However, except with zsh, ${array[1]} won't be the same as $1.

For that, you'd need:

array=(x "$@")
unset "array[0]"

Note that zsh arrays are compatible with the positional
parameters. They are not sparse and they're indexed from 1.
Actually, in zsh the positional parameters are also accessible
via the $argv special array like in csh. So $#argv is the same
as $#.

For an even better design, look at the "rc" shell. All variables
are lists (not sparse arrays), $* is the list holding the
positional parameters. $1 is the same as $*(1), and the element
count is $#*.

--
Stephane

0 new messages