echo命令是否会在输入字符串的末尾自动添加换行符? [英] Does echo command automatically add a new line character to the end of the input string?
问题描述
所以我才刚刚开始学习bash脚本.我在一本书中遇到了一个问题.测试文件示例包含以下内容.
So I was just starting learning bash scripting. I encountered a question in a book. An example testfile contains following content.
$ cat testfile
This is the first line.
This is the second line.
This is the third line.
脚本文件如下:
#!/bin/bash
# testing input/output file descriptor
exec 3<> testfile
read line <&3
echo "Read: $line"
echo "This is a test line" >&3
运行脚本后,测试文件变为:
After running the script, the testfile became:
$ cat testfile
This is the first line.
This is a test line
ine.
This is the third line.
我了解为什么该脚本会更改测试文件.我的问题是为什么我."从新的一行开始?echo命令是否会自动在字符串末尾添加换行符?
I understand why that script changes the testfile. My question is why "ine." starts from a new line? Does echo command automatically add a newline character to the end of the string?
推荐答案
echo -n
是您要寻找的:选项-n指示echo不输出结尾的换行符".
echo -n
is what you seek: the option -n
instructs echo to "do not output the trailing newline".
FWIW:您平台上的 man echo
将指示/bin/echo命令可以理解的选项.但是由于您将bash称为shell:bash具有echo的内部实现(所谓的内置")
FWIW: man echo
on your platform will instruct what options the /bin/echo command understands. But since you mention bash as shell: bash has an internal implementation of echo (a so-called "builtin")
这篇关于echo命令是否会在输入字符串的末尾自动添加换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!