![如何在腳本命令中將十六進位字元傳遞給 printf?](https://rvso.com/image/231049/%E5%A6%82%E4%BD%95%E5%9C%A8%E8%85%B3%E6%9C%AC%E5%91%BD%E4%BB%A4%E4%B8%AD%E5%B0%87%E5%8D%81%E5%85%AD%E9%80%B2%E4%BD%8D%E5%AD%97%E5%85%83%E5%82%B3%E9%81%9E%E7%B5%A6%20printf%EF%BC%9F%20.png)
從腳本執行時如何讓 printf 輸出十六進位字元?
假設我在提示符號處輸入 printf "\x41\x42"我得到的輸出
AB%
但是,如果我有一個包含該行的腳本,即 cat test.sh
產生
printf "\x41\x42"
但是執行腳本./test.sh產生
\x41\x42%
如何讓腳本產生與 shell 提示符號相同的結果?
答案1
嘗試使用外部printf
實用程式:
/usr/bin/printf "\x41\x42"
您可能有一個bash
shell,但作為互動式 shell, whilesh
連結到dash
.
dash
內建函數不printf
知道\xNN
,因此您必須使用八進位值 ,\0NN
來代替。
內建bash
確實printf
知道\xNN