So I have a problem below, but my question is more generic - how to see exact content of memory referenced by bash variable to understand why they don't match:
# [[ $c1 == $c ]] || echo nope
nope
# [[ $c1 == "bkup dt" ]] || echo nope
# [[ $c == "bkup dt" ]] || echo nope
nope
# hexdump -C <<<$c
00000000 62 6b 75 70 20 64 74 0a |bkup dt.|
00000008
# hexdump -C <<<$c1
00000000 62 6b 75 70 20 64 74 0a |bkup dt.|
00000008
# [ "$c1" = "$c" ] || echo nope
nope
# [ ! "$c1" = "$c" ] || echo nope
Or does it look like a bug? I can repeat the problem with:
$ cd /tmp
$ mkdir aaa
$ echo 2 > aaa/1
$ echo 2 > aaa/2
$ c=$(ls -A aaa)
$ [[ $c == $(echo $c) ]] || echo not equal
not equal
$ hexdump -C <<<$c
00000000 31 20 32 0a |1 2.|
00000004
$ hexdump -C <<<$(echo $c)
00000000 31 20 32 0a |1 2.|
00000004
$ c1="1 2"
$ [[ $c1 == $(echo $c1) ]] || echo not equal
$ [[ $c1 == $(echo $c) ]] || echo not equal
$ [[ $c1 == $c ]] || echo not equal
not equal
The best thing to inspect the content of a variable is to use
declare -p
:Note that your tests are wrong because you're missing out quotes in your variable expansions!
Look:
You must quote every single variable expansion, unless you really want to have word splitting and pathname expansion applied to them! (and usually, you certainly don't want that to happen).
Even with your
hexdump
strategy you need quotes:What you're experiencing is exactly this:
Sometimes
declare -p
will not quite show spaces properly. In this case you can useprintf
like so:The declare strategy is also nice as you can inspect arrays and functions too:
You also have access to the flags of the variables: