I have a giant script that I'm attempting to debug, and in it it has the following construction:

var "-p Some Parameter"  || return 1
var "-p Other Parameter" || return 0

I want to know what it actually executes (and I know it executes something since I get error message from whatever var is), but when I do echo $var or echo ${var} etc, it gives me empty strings. echo "${!var}" doesn't work ("syntax error: bad substitution") and if I do

eval e="\$$var"
echo $e

it just prints "$"

Is there any way to find out what a variable is actually equal to in ash, or is there some sort of tracing that would allow me to see what actual commands the script executes (that's available on Alpine)?

1 Answer 1

var "-p Some Parameter"  || return 1

ash should be a POSIX-like shell, so this just runs a command called var, with the string -p Some Parameter as an argument (and then possibly calls return based on the exit status). What happens depends entirely on what var does.

when I do echo $var or echo ${var} etc, it gives me empty strings.

Then the variable $var is probably unset or empty (or only contains whitespace). You could try echo "${var+var is set}" to see if it's set at all, if that matters.

But a variable called var has nothing to with a command or function called var.

echo "${!var}"

This is a Bashism for indirect variable expansion, it would expand to the value of the variable named in the variable var. It's not standard so it may well result in an error.

eval e="\$$var"
echo $e

I'm not sure what this is supposed to do: it seems it would just assign the value of the variable var to variable e, and then print it, except that the assignment breaks if the variable contains white space.

You must log in to answer this question.

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