Assign string containing null-character (\0) to a variable in Bash

后端 未结 4 748
孤城傲影
孤城傲影 2020-11-30 03:42

While trying to process a list of file-/foldernames correctly (see my other questions) through the use of a NULL-character as a delimiter I stumbled over a strange behaviour

4条回答
  •  独厮守ぢ
    2020-11-30 04:20

    Use uuencode and uudecode for POSIX portability

    xxd and base64 are not POSIX 7 but uuencode is.

    VAR="$(uuencode -m <(printf "a\0\n") /dev/stdout)"
    uudecode -o /dev/stdout <(printf "$VAR") | od -tx1
    

    Output:

    0000000 61 00 0a
    0000003
    

    Unfortunately I don't see a POSIX 7 alternative for the Bash process <() substitution extension except writing to file, and they are not installed in Ubuntu 12.04 by default (sharutils package).

    So I guess that the real answer is: don't use Bash for this, use Python or some other saner interpreted language.

提交回复
热议问题