[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: consistency probs var & function re-use

From: Greg Wooledge
Subject: Re: consistency probs var & function re-use
Date: Tue, 13 Jun 2017 08:07:37 -0400
User-agent: Mutt/

On Sun, Jun 11, 2017 at 03:51:26PM -0700, L A Walsh wrote:
> I was pointing out that the reason 'declare' used a newline, was that he
> had not quoted the input to 'x', which is expanded to a newline
> before it is assigned to 'x'.

You are completely wrong here.  My variable 'x' had a newline in its
content.  I used the $'...' quoting syntax to enter it as such.

Your variable did not have a newline in it, because you used "..."

The result of declare -p x is a bash command that you can eval (or
source) to recreate the variable x with the same content and attributes.
(But not scope, of course.  It doesn't encode scope information.)

Bash is free to use a different kind of quoting around the variable
content in declare -p's output.  I don't care that bash chooses a
different formatting than I used.  The end result is the same variable,
so it simply doesn't matter.

>   hh(){ echo "hi"; }
> which gets printed out as:
>  declare -f hh
>  hh ()
>  {
>      echo "hi"
>  }

But it's the *same function*.  It's just got different formatting.
Given the same arguments, both of them execute the same commands.
This is the definition of reusability that we're working with.

Does it reproduce your exact formatting?  No.  It's not intended to.
In fact, some people *like* it this way.  It's a pretty-printer for

> I.e. it wasn't that I wanted to embed newlines in the function, but that
> I wanted it to print out the same way it was input.

Then store it in a file and source that file every time you want to use
it.  If you want to see your gorgeous whitespace formatting, cat the file.

declare -fp is not a guarantee of whitespace preservation.  It's merely
a guarantee that the resulting function works the same way.

reply via email to

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