我四处看了看,我感到绝望
我的文件是这个
mkdir -p ~/Desktop/new_file && echo "hello\\world > ~/Desktop/new_file.txt
Run Code Online (Sandbox Code Playgroud)
我也试过
hello\n world
hello ; world
Run Code Online (Sandbox Code Playgroud)
我正在尝试从终端完成一个多行句子的返回信号到我的 hello world 输出
的手册页echo显示以下内容:
DESCRIPTION
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
Run Code Online (Sandbox Code Playgroud)
这意味着如果您想要多行输出,您只需从echo -e并添加一个\nfor 新行。
mkdir -p ~/Desktop/new_file && echo -e "hello\nworld" >> ~/Desktop/new_file/new_file.txt
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
如果我正确理解你的问题,你想使用:
echo "hello" > ~/Desktop/new_file.txt && echo "world" >> ~/Desktop/new_file.txt
Run Code Online (Sandbox Code Playgroud)
然后检查结果,使用cat ~/Desktop/new_file.txt显示:
hello
world
Run Code Online (Sandbox Code Playgroud)
有一个更短的方法可以做到这一点,但我自己对 Linux 有点陌生。
首先,虽然你的标题提到了touch,但你实际使用的命令是mkdir这样你创建了一个名为 的目录new_file。您将无法按new_file原样写入文本。
事实上,不需要在单独的步骤中创建目标文件:将命令的标准输出重定向到命名文件将自动创建它(如果该文件尚不存在)。您可以使用删除(空)目录 new_file rmdir ~/Desktop/new_file
出于此处概述的原因为什么 printf 比 echo 更好?你可能想考虑使用
printf 'Hello\nworld\n' > ~/Desktop/new_file
Run Code Online (Sandbox Code Playgroud)
或使用此处的文档
cat > ~/Desktop/new_file
Hello
world
Run Code Online (Sandbox Code Playgroud)
它允许您直接输入多行文本,完成后用Ctrl+终止输入。D
在任何一种情况下,如果您想要追加而不是覆盖文件的现有内容,则可以替换为>。>>
| 归档时间: |
|
| 查看次数: |
40286 次 |
| 最近记录: |