本文翻译自:'echo' without newline in a shell script

I have a problem with echo in my script: 我的脚本中存在echo问题:

echo -n "Some string..."

prints 版画

-n Some string...

and moves to the next line. 并移至下一行。 In the console it's working correcly without newline: 在控制台中,它不需要换行就可以正常工作:

Some string...

#1楼

参考:https://stackoom.com/question/kxvm/在shell脚本中没有换行符的-echo


#2楼

There are multiple versions of the echo command, with different behaviors. echo命令有多个版本,具有不同的行为。 Apparently the shell used for your script uses a version that doesn't recognize -n . 显然,用于脚本的外壳程序使用的版本无法识别-n

The printf command has much more consistent behavior. printf命令具有更一致的行为。 echo is fine for simple things like echo hello , but I suggest using printf for anything more complicated. echo适用于诸如echo hello类的简单事情,但我建议将printf用于更复杂的事情。

What system are you on, and what shell does your script use? 您在什么系统上,脚本使用什么外壳?


#3楼

Try with 试试看

echo -e "Some string...\c"

It works for me as expected (as I understood from your question). 正如我从您的问题中了解到的那样,它对我有效。

Note that I got this information from the man page. 请注意,我是从man页获得此信息的。 The man page also notes the shell may have its own version of echo , and I am not sure if bash has its own version. man页还指出,shell可能具有自己的echo版本,并且我不确定bash是否具有自己的版本。


#4楼

bash has a "built-in" command called "echo": bash有一个名为“ echo”的“内置”命令:

$ type echo
echo is a shell builtin

Additionally, there is an "echo" command that is a proper executable (that is, the shell forks and execs /bin/echo , as opposed to interpreting echo and executing it): 此外,还有一个“ echo”命令,该命令是适当的可执行文件(即,与解释和执行echo相反,shell派生和execs /bin/echo ):

$ ls -l /bin/echo
-rwxr-xr-x 1 root root 22856 Jul 21  2011 /bin/echo

The behavior of either echo 's WRT to \\c and -n varies. echo的WRT到\\c-n各不相同。 Your best bet is to use printf , which is available on four different *NIX flavors that I looked at: 最好的选择是使用printf ,它在我看过的四种不同的* NIX风格中可用:

$ printf "a line without trailing linefeed"
$ printf "a line with trailing linefeed\n"

#5楼

如果您在if命令中与其他命令(例如“ read”)一起使用echo,则它可能会忽略该设置,并且无论如何都会跳到新行。


#6楼

Just for the most popular linux Ubuntu & it's bash : 仅适用于最受欢迎的Linux Ubuntubash

  1. Check which shell are you using? 检查您使用的是哪个外壳? Mostly below works, else see this : 大多在下面的作品,否则看到这个 :

    echo $0

  2. If above prints bash , then below will work: 如果上面打印bash ,那么下面的将起作用:

    printf "hello with no new line printed at end"
    OR 要么
    echo -n "hello with no new line printed at end"

在shell脚本中没有换行符的#39;echo#39;相关推荐

  1. 如何在sh的字符串中包含换行符?

    本文翻译自:How can I have a newline in a string in sh? This 这个 STR="Hello\nWorld" echo $STR pro ...

  2. linux shell脚本字符串连接符,学习Linux shell脚本中连接字符串的方法

    这篇文章主要介绍了Linux shell脚本中连接字符串的方法,如果想要在变量后面添加一个字符,可以用一下方法: 代码如下: $value1=home $value2=${value1}"= ...

  3. linux过滤脚本中的字段,Shell脚本中常用的文本过滤命令

    在Linux运维日常工作中,Shell脚本的使用如家常便饭一样,须做到顺手拈来,文本处理所占比重更是不容小视,而文本处理中,稍具难度的莫过于文本的过滤.今天我们要通过这一讲,掌握文本过滤的常用命令,有 ...

  4. shell脚本中一些特殊符号

    shell脚本中一些特殊符号 在shell中常用的特殊符号罗列如下: # ;   ;; . , / \\ 'string'| !   $   ${}   $? $$   $* \"strin ...

  5. shell脚本中的结构化命令(if-then-else、case、for、while、until) 脚本中的循环控制

    1. 结构化命令 上一次我们学习了shell脚本的一些基础知识,包括环境变量.重定向.数学运算.退出脚本的方式等,想了解的可以戳这个: shell脚本基础 之前,在我们的示例shell脚本里,shel ...

  6. shell脚本中的一些特殊符号

    shell脚本中一些特殊符号 在shell中常用的特殊符号罗列如下: # ;   ;; . , / \\ 'string'| !   $   ${}   $? $$   $* \"strin ...

  7. [shell] widows与Linux 换行符不同 导致字符串拼接错误

    场景: 在shell中读取配置文件,并将每一行增量赋值给变量,即进行字符串的拼接.之后对变量进行操作 问题描述 编写好脚本后,执行脚本,只能得到最后一行配置信息 config.cfg config1 ...

  8. Linux系统Shell脚本中的echo命令

    Linux系统Shell脚本中的echo命令 echo的作用       输出字符串 格式                  echo  "字符串内容" 一,显示转义字符 转义字符 ...

  9. shell脚本中$0,$1,$2的含义

    我们经常见到shell脚本中使用的$0,$1,$2,分别的意思就是: $0:是指你所写的shell脚本本身的名字: $1:是指你写的shell脚本所传入的第一个参数 : $2:是指你写的shell脚本 ...

最新文章

  1. Sudo 漏洞隐患不断,macOS 也受牵连!
  2. 在正确的方向坚持下去,一直坚持下去,直到有成果
  3. 控制文件中的 MAXDATAFILES 参数
  4. autojs定时可以选定日期吗_Excel工作表中的7个“一键完成”,你真的都了解、掌握吗?...
  5. 机房系统(一)——【修改密码 登录 】
  6. 在C#中使用Json.Net进行序列化和反序列化及定制化
  7. java concurrent 例子_[Java Concurrent] 并发访问共享资源的简单案例
  8. A5营销:传统企业站优化正文内容在SEO方面的三大注意事项
  9. windows服务器管理(1)——WinServer2012 开启中文语言包(无需下载补丁)
  10. (1)外网映射(NATAPP快速入门)
  11. yate怎样调出彩色的log日志实时调试信息
  12. 获取 点集合 的 rect
  13. Unity ASE点光源不显示的问题
  14. 2021-12-21 理解JS中的shim / polyfill / 垫片概念
  15. 实体店为什么难以留住客户?商业模式值得尝试
  16. windows10批处理文件设置IP地址
  17. 通俗的角度理解遍历性定理 (从大数定理,中心极限定理再到遍历性定理)
  18. oracle em13c install,如何使用Oracle EM13c管理数据库私有云
  19. JAVA实现闯关小游戏(一)
  20. acl中的反掩码,acl掩码匹配规则

热门文章

  1. 接口转发和重定向区别(三)
  2. 【数据库】基础知识总结
  3. 【剑指offer-Java版】30最小的K个数
  4. Android动态申请多个权限-非第三方框架
  5. activity在屏幕旋转时的生命周期
  6. 计算机基础知识自考真题,自考《计算机应用基础》基础试题
  7. Android之DiskLruCache源码解析
  8. (014)java后台开发之用tomcat发布自己的javaWeb项目
  9. postman断言测试脚本二 (对数据格式和内容匹配测试)
  10. 巴士云_“相见不如在线”巴士五公司“云面试”汽修工和驾驶员