From help declare:
-p display the attributes and value of each NAME
Just running declare -p works as expected. So does running it in a subshell with (declare -p).
However, when using echo "$(declare -p)", i.e. executing the command in a subshell, catching stdout and echoing that to stdout again, I noticed something weird happening: With each call, the output of declare -p gets longer and additional backslashes appear. The execution also takes increasingly longer.
Output of first run of echo "$(declare -p)":
...
declare -x SHELL="/bin/bash"
...
Second run:
...
declare -x SHELL="/bin/bash"
...
declare -x SHELL=\"/bin/bash\"
...
Third run:
...
declare -x SHELL="/bin/bash"
...
declare -x SHELL=\"/bin/bash\"
...
declare -x SHELL=\\\"/bin/bash\\\"
...
What's going on here?
declare -- _="-- gnu.org/software/bash/manual/bash.html#index-_005f_variable! I would've never thought of that. Thank you very much for your help :)