在Bash中使用heredoc创建新文件

问题描述 投票:-1回答:1

对不起,如果有人已经提出这个问题,我搜索了它并且还没有找到答案。我需要通过一个命令行使用heredoc创建一个新的文本文件。到目前为止我没有成功的尝试,像这样 -

cat << "" >> newfile.txt

谢谢

linux bash ubuntu heredoc
1个回答
2
投票

三重引号表示一个herestring:

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

没有理由使用herestring。一种更简单的方法是:

# 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
© www.soinside.com 2019 - 2024. All rights reserved.