0

I want to declare a few non environment variables and print them directly afterwards.

For example:

read domain &&
web_application_root="${HOME}/www" &&
domain_dir="${web_application_root}/${domain}/public_html" &&

What command should come after the third && to print the output of the last three variable declarations?

The purpose of printing the output is to just neatly show the output of the three commands in one place, ordinal, perhaps in a table-like manner, comfortable to read (much more comfortable than say set -x traces).

2

1 Answer 1

0

You could define a function to do it. Here I'm using a bash function, if you're using a different shell you might have to tweak it:

printVariables() {
    local maxLen=0

    # Figure out the length of the longest variable name
    for i; do
        if ((${#i} > maxLen)); then
            maxLen=${#i}
        fi
    done

    # Make room for the colon
    maxLen=$((maxLen + 1))

    # Print the named variables
    for i; do
        printf "%-${maxLen}s %s\n" "${i}:" "${!i}"
    done
}

Then:

$ read domain &&
web_application_root="${HOME}/www" &&
domain_dir="${web_application_root}/${domain}/public_html" &&
printVariables domain web_application_root domain_dir
example.com

Will produce the following output:

domain:               example.com
web_application_root: /home/user/www
domain_dir:           /home/user/www/example.com/public_html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .