Search code examples
bashbinaryechomode

Bash : echo binary output


for a script i must create a file and add a line (only numeric) to this. But this should be in binary mode, but I havent found a solution.

My actually commands:

SIZE=200
touch quota
echo $SIZE >> quota

How can do this in binary mode?


Solution

  • Perhaps you need to use the -n option?

    echo -n "$SIZE" >> quota
    

    Or maybe you need the binary representation it but this is only limited to 8 bits or 255.

    echo -ne "$(printf '\\x%x' 200)" >> quota
    

    Also make sure that you really need to use >> and not > as >> appends data to existing files, not overwrite it.