function hex-var()
{
echo "--- ${(r:25::-:: :)1}"
if [[ -v $1 ]]; then
set -- "${(@P)1}"
print -rC1 -- ${(q+)@}
echo "\n-----------------------------\n"
for element ("$@") print -rn -- $element | od -vAx -tx1 -tc
else
echo "No such variable: ${(q+)1}"
fi
}
And, all else equal it's probably more othodox to pass the value of the
variable than the name.
It all depends on what you want to do. For instance, if you want to see exactly what's in a variable, you better pass its name to a function like hex-var than (try to) pass its value to a function like hex-doors because with Zsh chances are high that if you try to attempt the latter you will fail.
Here is a first example of failure:
% var=""; hex-var var; hex-doors $var
--- var ---------------------
''
-----------------------------
hex-var correctly shows that var contains the empty string but hex-doors see nothing at the front door. The "problem" is that Zsh entirely drops $var if it expands to an empty string. Thus, in this case, hex-doors is called with no arguments instead of with a single empty string.
You can fix the "problem" by quoting $var, then hex-doors also sees the empty string:
% var=""; hex-var var; hex-doors "$var"
--- var ---------------------
''
-----------------------------
--- Front door --------------
''
-----------------------------
Here is another example of where $var fails to pass the right value(s) to hex-doors:
% var=(aa "" bb cc); hex-var var; hex-doors $var
--- var ---------------------
aa
''
bb
cc
-----------------------------
0000000 61 61
a a
0000002
0000000 62 62
b b
0000002
0000000 63 63
c c
0000002
--- Front door --------------
aa
bb
cc
-----------------------------
0000000 61 61
a a
0000002
0000000 62 62
b b
0000002
0000000 63 63
c c
0000002
Again, hex-doors doesn't see the empty string. The "problem" is again that when Zsh epands $var and replaces it by its values, it drops any empty string. Thus, it calls hex-doors with 3 strings: "aa", "bb", and "cc", instead of calling it with the 4 values stored inside var. If we apply the same trick as above and quote $var, we still don't get the right result:
% var=(aa "" bb cc); hex-var var; hex-doors "$var"
--- var ---------------------
aa
''
bb
cc
-----------------------------
0000000 61 61
a a
0000002
0000000 62 62
b b
0000002
0000000 63 63
c c
0000002
--- Front door --------------
'aa bb cc'
-----------------------------
0000000 61 61 20 20 62 62 20 63 63
a a b b c c
0000009
Here you can see that the empty string was taken into account because there are two spaces between aa and bb, while there is only one between bb and cc. Unfortunately, all the values were jammed together. That's because the default behavior of quotes on array values is to join their values with spaces between them. To prevent this joining, we can use the (@) expansion flag, which tells Zsh to keep array values separated:
% var=(aa "" bb cc); hex-var var; hex-doors "${(@)var}"
--- var ---------------------
aa
''
bb
cc
-----------------------------
0000000 61 61
a a
0000002
0000000 62 62
b b
0000002
0000000 63 63
c c
0000002
--- Front door --------------
aa
''
bb
cc
-----------------------------
0000000 61 61
a a
0000002
0000000 62 62
b b
0000002
0000000 63 63
c c
0000002
Now, hex-doors finally see the same as hex-var. And the good thing is that the same _expression_ also works for the string variable:
% var=""; hex-var var; hex-doors "${(@)var}"
--- var ---------------------
''
-----------------------------
--- Front door --------------
''
-----------------------------
What this shows is that you can indeed use hex-doors to see what's inside a variable but it requires being very careful on how you pass that variable's value to hex-doors. If your aim is to see what's inside a variable, it's much more reliable to pass its name to hax-var.
Now, to see whether you better understand what's going on, you can try to guess what's the result of the following three calls to hex-doors:
% var=(aa "" bb cc)