Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 14 Jul 2002 23:04:30 +0300
From:      Giorgos Keramidas <keramida@FreeBSD.org>
To:        "Balaji, Pavan" <pavan.balaji@intel.com>
Cc:        "'freebsd-questions@freebsd.org'" <freebsd-questions@FreeBSD.org>
Subject:   Re: argc/argv in bash!
Message-ID:  <20020714200430.GB37859@hades.hell.gr>
In-Reply-To: <3D386AED1B47D411A94300508B11F18703BC5BA4@fmsmsx116.fm.intel.com>
References:  <3D386AED1B47D411A94300508B11F18703BC5BA4@fmsmsx116.fm.intel.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2002-07-14 12:53 +0000, Balaji, Pavan wrote:
> 
> How do we use argc and argv (C like) in bash scripts?

The number of command line arguments is $#.  For example:

	% cat foo.sh
	#!/bin/sh
	echo $#

	% sh foo.sh
	0

	% sh foo.sh hello world
	2

	% sh foo.sh "hello world"
	1

The argv[] equivalent is $@.

	% cat foo2.sh
	#!/bin/sh

	for argument in $@ ;do
		echo $argument
	done

	% sh foo2.sh
	%

	% sh foo2.sh hello world
	hello
	world
	%

	% sh foo2.sh "hello world"
	hello world
	%

For more details about the "special variables" of sh, read the sh(1)
manpage.

	% man 1 sh


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020714200430.GB37859>