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: Peng Yu
Subject: Re: [Help-bash] How to print a bash variable in the most succinct way?
Date: Mon, 15 Aug 2016 22:36:23 -0500

On Wed, Aug 10, 2016 at 7:40 AM, Greg Wooledge <address@hidden> wrote:
> On Wed, Aug 10, 2016 at 05:47:03AM -0500, Peng Yu wrote:
>> I need the most human readable repesentation. The one returned by declare
>> has redundancy in it. E.g., [1]= is not really necessary.
>
> 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.

https://www.gnu.org/software/parallel/env_parallel.html

env_parallel uses something like `declare -p` to extract the current
environment so that the variables and functions in the current bash
can be used to parallel sessions. But env_parallel also remove
variables like BASH_* with grep as they should not be inherented in
child parallel session.

However, grep would not work if the content of a variable contains
newlines.  For example, the autogenerated command by `declare -p` will
be of multiple lines and grep can not easily recognize it.

~$ x=($'a\nb' $'c\nd')
~$ declare -p x
declare -a x='([0]="a
b" [1]="c
d")'

Ideally, for this case, one would like the output of `declare -p` be

declare -a x="([0]=$'a\nb' [1]=$'c\nd')"

As one can imagine, there can be many special cases involved. I had
tried to make a general program to reformat the output generated by
`declare -p`. I don't think this is easy to do.

Chet, do you see an easy way to solve this problem?

-- 
Regards,
Peng



reply via email to

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