-1

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

jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    What do you mean by "only one command line"? In a single command on single line, or in a single command that may span multiple lines? – that other guy Mar 14 '19 at 18:38
  • [How can I write a heredoc to a file in Bash script?](https://stackoverflow.com/q/2953081/608639), [Open and write data to text file using Bash?](https://stackoverflow.com/q/11162406/608639), [Echo a large chunk of text to a file using Bash](https://stackoverflow.com/q/6896025/608639), etc. – jww Mar 15 '19 at 02:56
  • Can you not just `touch` a file? – Major Mar 15 '19 at 03:03

1 Answers1

2

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
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Thanks a lot, I'm sorry if I was not clear. I want to create a text file using heredoc (<<). I know the ways you offer but I need to do that with heredoc in on command. – user11204821 Mar 15 '19 at 16:09