Greg Wooledge wrote:
What are you talking about? Consistency between what two things?
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"
Is that "consistent" enough for you?
----
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'"
However, if that is your example showing dcl-p's rule for inserting
newlines, then given this funcdef:
hh(){ echo "hi";}
There shouldn't be any newlines (only inserting them when user
inserted a newline via $'\n'). However, in bash's output of
that function:
> alias my=declare pf='my -pf'
> pf hh
hh ()
{
echo "hi"
}
---
Oops... superfluous newlines were added :-(
Remember, a newline only takes 1 byte of memory.
The output is reusable, but only if you actually use it correctly. E.g.
stuff it into a variable and use eval "$myvar" with the double quotes.
Or redirect it into a file and use source ./myfile.
What are you trying to do?
Read var & func defs via a 'read' of 1 line.
What did you try?
x="$'foo\nbar'"
read -r mydef< <(declare -p x)
echo "$mydef"
hh(){ echo hi;}
read -r mydef< <(declare -pf hh)
echo "$mydef"
What happened?
1st case worked:
declare -- x="\$'foo\nbar'"
2nd case didn't:
hh ()
What did you expect to happen instead?
If there was a consistency, then would have expected both to work.