[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] How to print a bash variable in the most succinct way?
From: |
Matthew Cengia |
Subject: |
Re: [Help-bash] How to print a bash variable in the most succinct way? |
Date: |
Thu, 11 Aug 2016 00:53:21 +1000 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On 2016-08-10 11:04, Marco Silva wrote:
> Excerpts from Greg Wooledge's message of 2016-08-10 08:40:24 -0400:
> > Stop complaining that autogenerated code is less elegant than
> > human-written code. Write the code yourself if you don't like what a
> > computer wrote for you.
>
> You are wrong. As well as impolite. He is not complaining about
> auto-generated code.
>
> Actually, Mr. Yu has one good question about an idiom to construct
> an array out of a list of values. Unfortunately, shell scripts
> deals with many forms of lists, and arrays are really not a data
> structure in shell. They were introduced to late, and they are clumsy to
> retrieve values from.
>
> You can compare:
>
> set a b c; while [[ -n $1 ]]; do echo $1; shift; done
>
> With
>
> a=(a b c); i=0; while [[ $i -le 2 ]]; do echo ${a[$i]}; i=$(($i+1)); done
>
> As you can see, arrays declaration is something cheap, just a list of
> values separated by a $IFS . But, to retrieve it you must use a clumsy
> syntax, which many authors avoid to use in favor of positional
> parameters.
Greg is, in fact, correct.
The OP is complaining about the output of 'declare -p', which produces
an auto-generated array declaration, and is asserting that the
auto-generated array declaration should look a particular way.
Additionally, your own assertion that printing arrays is difficult is
incorrect:
a=(a b c); printf "%s\n" "address@hidden"
I agree that Greg is very direct and calls things as they are, and that
may be seen as impoliteness, but I also don't blame them for doing so,
given the circumstances.
--
Regards,
Matthew Cengia
signature.asc
Description: Digital signature