【问题标题】:Create new file using heredoc in Bash在 Bash 中使用 heredoc 创建新文件
【发布时间】:2019-08-05 18:13:22
【问题描述】:

很抱歉,如果有人已经问过这个问题,我已经搜索过了,但还没有找到答案。 我只需要通过一个命令行使用 heredoc 创建一个新的文本文件。 到目前为止我尝试过的都没有成功,就像这样-

cat << "" >> newfile.txt

谢谢

【问题讨论】:

标签: linux bash ubuntu heredoc


【解决方案1】:

三引号表示此处的字符串:

# append blank line, create if it doesn't exist
cat <<< "" >> newfile.txt

没有理由使用这里字符串。更简单的方法是:

# append blank line, create if it doesn't exist
echo >> newfile.txt

你意识到这两个都在文件中附加了一个空行吗?如果您只是想创建一个大小为 0 的完全空文件,请改为:

# create empty file, truncate if it already exists
> newfile.txt

如果文件已经存在,它将截断文件。如果您只是想确保一个文件存在,但如果它已经存在则不要管它:

# create empty file, do nothing if it already exists
touch newfile.txt

【讨论】:

  • 非常感谢,如果我不清楚,我很抱歉。我想使用 heredoc (
猜你喜欢
  • 2014-01-04
  • 2019-03-05
  • 2014-08-29
  • 2015-02-02
  • 2013-09-26
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多