简体   繁体   中英

Create new file using heredoc in Bash

I'm sorry if anyone has already asked the question, I searched for it and have not yet found an answer. I need to create a new text file using heredoc by only one command line. What I have tried so far without success, something like this-

cat << "" >> newfile.txt

thanks

Triple quotes denote a herestring:

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

There's no reason to use the herestring. A simpler way to do it would be:

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

You realize both of those are appending a blank line to the file? If you're just trying to create a completely empty file with size 0, do this instead:

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

That will truncate the file if it already exists. If you just want to ensure a file exists but leave it alone if it already does:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM