See the exact contents of the bash variable? (hexdump does not help)

So, I have a problem below, but my question is more general - how to see the exact memory content referenced by the bash variable in order to understand why they do not 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 seem like a mistake? 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
+4
source share
2 answers

The best way to check the contents of a variable is to use declare -p:

$ c="1 2"
$ declare -p c
declare -- c="1 2"

Note that your tests are incorrect because you are missing quotation marks in your variable extensions!

Appearance:

$ c="1  2" # with two spaces
$ declare -p c
declare -- c="1  2"
$ echo $c
1 2
$ echo "$c"
1  2
$ d=$(echo $c)
$ declare -p d
1 2

, ! (, , , , , ).

hexdump :

$ c="1  2" # two spaces
$ hexdump <<< $c
00000000  31 20 32 0a                                       |1 2.|
00000004
$ hexdump <<< "$c"
00000000  31 20 20 32 0a                                    |1  2.|
00000005

, , :

$ mkdir aaa; touch aaa/{1,2}
$ c=$(ls -A aaa)
$ declare -p c
declare -- c="1
2"
$ # see? there a new line between the files.
$ echo $c
1 2
$ echo "$c"
1
2
$ # Use quotes!

declare -p . printf :

$ c=$'    \n'
$ declare -p c
declare -- c="    
"
$ # there are spaces, but you can't see them
$ printf '%q\n' "$c"
$'    \n'

, :

$ a=( one two "three four" )
$ declare -p a
declare -a a='([0]="one" [1]="two" [2]="three four")'
$ declare -A h=( [one]=1 [two]=2 )
$ declare -p h
declare -A h='([one]="1" [two]="2" )'
$ f() { echo hello; } > somewhere > >(over the rainbow)
$ declare -pf f
f () 
{ 
    echo hello
} > somewhere 2> >(over the rainbow)
$ # You need also the -f switch to target functions

:

$ declare -litux hello=world
$ declare -p hello
declare -itx hello="0"
$ # Have fun!
+10

, bash . :

$ echo hello # world
hello

$ echo "hello # world"
hello # world

Quoting # , , .

,

- .

a='a \b 
|c     d'   

, var a , .

$ echo "$a"
a \b 
|c     d

, var a - .

$a, - :

$ echo $a
a \b |c d

: .
, .


, , , , :

$ c='bkup dt
'

$ c1='bkup dt'

, c c1.

, , var (, , [[…]] ).

$ [[ $c1 == $c ]] || echo nope
nope

.

$ [[ $c1 == "bkup dt" ]] || echo nope

c1 bkup dt ( ).

$ [[ $c == "bkup dt" ]] || echo nope
nope

, c "bkup dt" ( ).

$ hexdump -C <<<"$c"
00000000  62 6b 75 70 20 64 74 0a  0a                       |bkup dt..|
00000009

, <<<.

$c1.

$ hexdump -C <<<"$c1"
00000000  62 6b 75 70 20 64 74 0a                           |bkup dt.|
00000009

!!.

, :

$ printf '%s' "$c" | od -vAn -tx1c
 62  6b  75  70  20  64  74  0a
  b   k   u   p       d   t  \n

.

$c1 , :

$ printf '%s' "$c1" | od -vAn -tx1c
 62  6b  75  70  20  64  74
  b   k   u   p       d   t

declare:

$ declare -p c
declare -- c="bkup dt
"

$ declare -p c1
declare -- c1="bkup dt"

cat, sed :

$ echo "$c" | cat -vET
bkup dt$
$

$ echo "$c1" | cat -vET
bkup dt$

$  echo "$c" | sed -n l
bkup dt$
$

$  echo "$c1" | sed -n l
bkup dt$

, .


.

$ cd /tmp; mkdir aaa; echo 2 > aaa/1; echo 2 > aaa/2
$ c=$(ls -A aaa)
$ echo $c | od -vAn -tx1c
  31  20  32  0a
   1       2  \n

c , , , :

$ echo "$c" | od -vAn -tx1c
  31  0a  32  0a
   1  \n   2  \n

A

, ( ):

$ echo $(echo $c) | odc
  31  20  32  0a
   1       2  \n

$ echo "$(echo "$c")" | odc
  31  0a  32  0a
   1  \n   2  \n
0

Source: https://habr.com/ru/post/1665411/


All Articles