How to write binary data in Bash

前端 未结 6 2048
后悔当初
后悔当初 2021-01-20 05:54

Let us say there is a variable in bash script with value \"001\" how can i write this binary data into a file as bits (as \"001\" not \"1\") echo writes it as string but i w

6条回答
  •  粉色の甜心
    2021-01-20 06:39

    Just use the %b in the printf:

    printf "%b" "\012"
    printf "%b" "\x0a"
    

    %b - Print the associated argument while interpreting backslash escapes in there

    so, above both prints binary value for the decimal 10.

    printf "%b" "\x0a" | od -bc
    

    output

    0000000   012                                                            
              \n      
    

    you can even mix

    printf "%b" "\12\xa\n" | od -bc
    
    0000000   012 012 012                                                    
              \n  \n  \n            
    

提交回复
热议问题