shell脚本echo输出不换行功能增强实例
这是第8/101个脚本
Thereareasmanywaystosolvethisquirkyechoproblemastherearepagesinthisbook.Oneofmyfavoritesisverysuccinct:
functionechon { echo"$*"|awk'{printf"%s"$0}' }
Youmayprefertoavoidtheoverheadincurredwhencallingtheawkcommand,however,andifyouhaveauser-levelcommandcalledprintfyoucanuseitinstead:
echon() { printf"%s""$*" }
Butwhatifyoudon'thaveprintfandyoudon'twanttocallawk?Thenusethetrcommand:
echon() { echo"$*"|tr-d'\n' }
Thismethodofsimplychoppingoutthecarriagereturnwithtrisasimpleandefficientsolutionthatshouldbequiteportable.
这个脚本很简单,就是可以用3种不同的函数(方法)来实现,输出后不换行。