help-bash
[Top][All Lists]
Advanced

[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: Greg Wooledge
Subject: Re: [Help-bash] How to print a bash variable in the most succinct way?
Date: Wed, 10 Aug 2016 10:56:11 -0400
User-agent: Mutt/1.4.2.3i

On Wed, Aug 10, 2016 at 11:04:56AM -0300, 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.

Yes, he is.  He is complaining about the output format of "declare -p".

> 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.

Not a single word of this has ANYTHING AT ALL to do with the output format
of "declare -p" or why the original poster is dissatisfied with it.

The purpose of "declare -p" is to autogenerate code that can be SOURCED
BY BASH to reconstruct a variable with all of its metadata.

It is not primarily intended for human eyes, though it is human-readable.

It is not intended to be pretty.

It is not intended to be elegant.

It is intended to solve a specific programmatic purpose: dumping a
variable now in order to reconstitute it later.

If I am impolite, so be it.  I'd rather be impolite than irrelevant.

I don't know how the original poster was trying to use the output of
"declare -p".  Maybe if he would TELL US HIS GOALS we could help him
more readily.

http://mywiki.wooledge.org/XyProblem
http://www.catb.org/~esr/faqs/smart-questions.html
http://mikeash.com/getting_answers.html

Also, you (Marco) are not even showing reasonable ways to iterate through
the contents of an array-that-is-treated-as-a-list.

# Iterating by value.
list=(a b c)
for val in "address@hidden"; do
  echo "$val"
done

# Iterating by index.
list=(a b c)
for idx in "address@hidden"; do
  echo "$idx -> ${list[idx]}"
done

# Passing the entire "list" as arguments to a command.
somecommand "address@hidden"

Pretty much every instance where you use an array to represent a list
should be using one of these three constructs.

(Still don't know what Peng wanted to do though.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]