Mac Shell脚本中使用echo的-n参数不生效

来源:互联网 发布:文件粉碎软件 编辑:程序博客网 时间:2024/06/04 00:44

Mac Shell脚本中使用echo -n导入文件无法实现不换行,在Linux服务器上未发现此问题

问题代码test.sh

#!/bin/bashtest_file="tmp"echo -n "line1" > ${test_file}echo -n "line2" >> ${test_file}echo -n "line3" >> ${test_file}#/bin/echo -n "line1" > ${test_file}#/bin/echo -n "line2" >> ${test_file}#/bin/echo -n "line3" >> ${test_file}

问题复现

使用命令在mac上执行: sh test.sh
查看文件tmp如下,不能实现不换行并且信息打印有误:

-n line1-n line2-n line3

解决方法

1.将echo替换为/bin/echo,即在示例脚本中注释掉内容
2.执行脚本时使用命令bash test.sh
3.给脚本执行权限,直接执行./test.sh

参考

https://stackoverflow.com/questions/11675070/makefile-echo-n-not-working

原创粉丝点击