Sample Header Ad - 728x90

Is the output of `declare -p <variable>` in bash guaranteed to be reusable as shell input?

5 votes
2 answers
640 views
This is specifically about bash's declare - the general case is pretty exhaustively dealt with in this answer (which mentions "the typeset/declare/export -p output of ksh93, mksh, zsh" but _not_ that of bash). Given a local/exported/array/assocative-array (but maybe not nameref) variable foo, is the output of declare -p foo in bash guaranteed to be reusable by bash? The official documentation doesn't mention anything like that: > The -p option will display the attributes and values of each name. > When -p is used with name arguments, additional options, other > than -f and -F, are ignored. And I looked through the CHANGES , and saw this about _functions_:
This document details the changes between this version, bash-2.05-beta1,
and the previous version, bash-2.05-alpha1.
...
b.  When `set' is called without options, it prints function definitions in a
    way that allows them to be reused as input.  This affects `declare' and
    `declare -p' as well.
And for a couple of other commands, -p is meant to produce reusable output:
s.  The shopt' -p' option now causes output to be displayed in a reusable
    format.
...
u.  umask' now has a -p' option to print output in a reusable format.
And Chet Ramey's Bash FAQ has:
Bash-2.0 contained extensive changes and new features from bash-1.14.7.
Here's a short list:
...
most builtins use -p option to display output in a reusable form
	(for consistency)
But nothing I can find about declare -p for variables.
Asked by muru (77471 rep)
Feb 15, 2024, 04:22 AM
Last activity: Feb 25, 2024, 10:55 AM