Skip to main content
added 67 characters in body
Source Link

Most solutions here either print only environment variables, or have the drawback that env or (set -o posix; posixset) do not print values in easily parseable form (try to print variable A=$'a\r\nb', it has multiple lines...).

Here is a function that will print all variables, one variable per line, in the POSIX escaped form (works properly for simple text variables, does not work for arrays):

function dump_vars {
    local VARNAME
    compgen -v | while read -r VARNAME; do
        printf "$VARNAME=%q\n" "${!VARNAME}"
    done
}

Thanks to @tmgoblin for pointing out the use of compgen -v.

Most solutions here either print only environment variables, or have the drawback that env or (set -o posix; posix) do not print values in parseable form (try to print variable A=$'a\r\nb'...).

Here is a function that will print all variables, one variable per line, in the POSIX escaped form:

function dump_vars {
    local VARNAME
    compgen -v | while read -r VARNAME; do
        printf "$VARNAME=%q\n" "${!VARNAME}"
    done
}

Thanks to @tmgoblin for pointing out the use of compgen -v.

Most solutions here either print only environment variables, or have the drawback that env or (set -o posix; set) do not print values in easily parseable form (try to print variable A=$'a\r\nb', it has multiple lines...).

Here is a function that will print all variables, one variable per line, in the POSIX escaped form (works properly for simple text variables, does not work for arrays):

function dump_vars {
    local VARNAME
    compgen -v | while read -r VARNAME; do
        printf "$VARNAME=%q\n" "${!VARNAME}"
    done
}

Thanks to @tmgoblin for pointing out the use of compgen -v.

Source Link

Most solutions here either print only environment variables, or have the drawback that env or (set -o posix; posix) do not print values in parseable form (try to print variable A=$'a\r\nb'...).

Here is a function that will print all variables, one variable per line, in the POSIX escaped form:

function dump_vars {
    local VARNAME
    compgen -v | while read -r VARNAME; do
        printf "$VARNAME=%q\n" "${!VARNAME}"
    done
}

Thanks to @tmgoblin for pointing out the use of compgen -v.