[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to supply a string with space in it as parameter to a function?
From: |
Peng Yu |
Subject: |
Re: How to supply a string with space in it as parameter to a function? |
Date: |
Wed, 23 Jun 2010 19:53:32 -0500 |
On Wed, Jun 23, 2010 at 5:43 PM, Chris F.A. Johnson
<chris@cfajohnson.com> wrote:
> On Wed, 23 Jun 2010, Peng Yu wrote:
>
>> Hi,
>>
>> According to man bash, I thought that $@ instead of $* can help me
>> pass a string with space as a parameter. But it is not. Would you
>> please show me how to print 'b c' as a single argument in the
>> following code?
>
> Always quote variable references unless you have a good reason not
> to.
>
>> #!/usr/bin/env bash
>>
>> function f {
>> #for i in $*;
>> for i in $@;
>> do
>> echo $i
>> done
>> }
>
> for i in "$@" ## note the quotes
> do
> echo "$i" ## better is: printf "%s\n" "$i"
> done
Why printf is better than echo? Is this because printf is more robust than echo?
--
Regards,
Peng