Bash:回显二进制输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18102350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:10:23  来源:igfitidea点击:

Bash : echo binary output

bashbinaryechomode

提问by mostworld77

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?

如何在二进制模式下做到这一点?

采纳答案by konsolebox

Perhaps you need to use the -n option?

也许您需要使用 -n 选项?

echo -n "$SIZE" >> quota

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

或者您可能需要二进制表示,但这仅限于 8 位或 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.

还要确保您确实需要使用 >> 而不是 > 因为 >> 将数据附加到现有文件,而不是覆盖它。