什么更便携?回声-e或使用printf吗? [英] What is more portable? echo -e or using printf?

查看:66
本文介绍了什么更便携?回声-e或使用printf吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个bash测试框架,该框架需要具有尽可能的可移植性.

I'm currently writing a bash testing framework, which requires to be as portable as possible.

因此,在某一点上,我必须先打印换行符,然后再打印一些包含多个扩展变量的文本.这是我使用的解决方案.

So, at one point, I had to print a leading newline followed by some text which contains several expanded variables. This is the solution I've used.

echo -e "\n$number_of_specs ${units}, $number_of_specs_failed failed"

这看起来还可以,但是我不确定与使用例如printf?

This seems okay but I'm not sure how portable that is compared to printing out the line using e.g. printf?

有什么想法或暗示可以在其中找到一些参考吗?

Any ideas or hints where I could find some references?

推荐答案

printf更具可移植性.始终应优先于echo -e.如果专门针对bash/zsh/ksh,echo $'...'也可以.如果专门针对zsh或ksh,则print没问题.

printf is more portable. It should always be preferred over echo -e. If targeting bash/zsh/ksh specifically, echo $'...' is also ok. If targeting zsh or ksh specifically, print is ok.

  • http://cfajohnson.com/shell/cus-faq.html#Q0b
  • http://www.in-ulm.de/~mascheck/various/echo+printf/
  • http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html#tag_20_37_16

POSIX(最后一个链接)还讨论了echo -n问题,也应避免这些问题.基本上,切勿使用echo的选项,并且出于可移植性,请使用printf.)

POSIX (last link) also discusses echo -n problems, which should also be avoided. Basically, never use options to echo, and for portability, use printf.)

这篇关于什么更便携?回声-e或使用printf吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆