0

GNU bash, version 4.3.42(1)-release

Trying to deliminate on a null character, but bash doesn't seem to keep the null character in a variable.

$ echo -e 'hello\0goodbye' > testlist
$ cat testlist | cut -d '' -f1
hello
$ foobar=$(echo -e 'hello\0goodbye'); echo "$foobar" | cut -d '' -f1
hellogoodbye

Is there something I'm doing wrong?

Miati
  • 405
  • 1
  • 5
  • 13
  • Why do you think that `''` is the null character? – Ignacio Vazquez-Abrams Nov 24 '16 at 06:37
  • 1
    @Miati, yes. According to my experience, shell cannot hold a null character in a variable. Other commands such as `find` and `xargs` have no problem. However, shell can even handle the null character as a part of a string, as you can see in the output of this command line: `echo -e 'A\0B' | od -tx1` – Jdamian Nov 24 '16 at 07:26
  • 2
    @Miati, I guess bash cannot keep the null character in a variable because it is used as the end-of-string delimiter character in C. – Jdamian Nov 24 '16 at 07:31

1 Answers1

1

Bash does embed the binary zero:

echo -e 'hello\0goodbye' | od -xc

gives:

0000000      6568    6c6c    006f    6f67    646f    7962    0a65        
       h   e   l   l   o  \0   g   o   o   d   b   y   e  \n  

although personally I prefer the \x00 notation.

It is the cut program which is the issue. So you can use the awk language instead. For example:

awk -F '\0'  '{print $1}' testlist

(note: no cat required) and:

foobar='hello\0goodbye'
echo -e "$foobar" | awk -F '\0'  '{print $1}'

both give:

hello

However, \0 is expanded to an empty string (as documented in man bash) so

foobar=$(echo -e 'hello\0goodbye')

looses the null, as you found.

cdarke
  • 42,728
  • 8
  • 80
  • 84