Charles Daffern wrote:
First problem: If you are assigning a string to a variable,
you need to put quotes around the string. That shows that "-p"
doesn't insert newlines:
> x="$'foo\nbar'"
> declare -p x
declare -- x="\$'foo\\nbar'"
You do not have any newlines in that string, so of course the
demonstration with -p will show no newlines.
Right. you should be addressing your comment to 'Greg Wooledge',
who said:
Are you allergic to newlines? declare -p uses raw newlines in its output
whenever it feels they are appropriate. Even in ordinary shell variables:
imadev:~$ x=$'foo\nbar'
imadev:~$ declare -p x
declare -- x="foo
bar"
------
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'.
Or did you miss that post?
What are you trying to do?
Read var & func defs via a 'read' of 1 line.
In your variable "x" above, you have encoded the string in a format
which contains no raw newlines and which can be "eval"ed to produce the
original contents.
Why don't you encode the function the same way?
I didn't encode the newline above, Greg did.
I used a format with NO newlines:
hh(){ echo "hi"; }
which gets printed out as:
declare -f hh
hh ()
{
echo "hi"
}
----
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.