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

后端 未结 4 764
孤城傲影
孤城傲影 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:32

    In Bash, you can't store the NULL-character in a variable.

    You may, however, store a plain hex dump of the data (and later reverse this operation again) by using the xxd command.

    VAR1=`echo -ne "n\0m\0k" | xxd -p | tr -d '\n'`
    echo -ne "$VAR1" | xxd -r -p | od -c   # -> 0000000    n  \0   m  \0   k
    

提交回复
热议问题