How to input text into a new text file using nano from command line?
You can use a here document
but with this way it is not possible to provide a special output document.
$ cat | nano <<-EOF
one
two
three
EOF
Received SIGHUP or SIGTERM
Buffer written to nano.save
This behaviour is mentioned in the man page under notes
In some cases nano will try to dump the buffer into an emergency file. This will happen mainly if nano receives a SIGHUP or SIGTERM or runs out of memory. It will write the buffer into a file named nano.save if the buffer didn't have a name already, or will add a ".save" suffix to the current filename. If an emergency file with that name already exists in the current directory, it will add ".save" plus a number (e.g. ".save.1") to the current filename in order to make it unique. In multibuffer mode, nano will write all the open buffers to their respective emergency files.
So i think nano is not the best choice for non interactive texting. If you only want to input multi line text to a file you can also use a here document
as well without nano.
cat > foo.txt <<-EOF
> one
> two
> three
>
> EOF
cme@itp-nb-1-prod-01 ~ $ cat foo.txt
one
two
three
Maybe this is what you need.
Related videos on Youtube
klor
Updated on September 18, 2022Comments
-
klor over 1 year
How to input text into a new text file using nano from command line?
I would like the same as with the following, but using nano:
echo 'Hello, world.' >foo.txt
Result:
- nano is not capable of handling non-interactive text input.
- echo is available in every Linux/Unix system, while nano is not installed by default in every Linux/Unix system. Echo can be also used in shell scripts, too.
Conclusion: The most compatible solution is to use
echo 'Hello, world.' >foo.txt
as solution to create a file and fill with input text non-interactively.
-
klor over 7 years@Serg: Yes, I think you are right, I have to stick with echo "Hello World" > foo.txt kind of solution.
-
JdeBP over 7 yearsDo not put answers in questions. That's not how this is supposed to work.
-
klor over 7 yearsSorry, but your solution is not done completely from command line. Thus I can not use is a shell script.
-
klor over 7 yearsSorry, but your solution is not done completely from command line. Thus I can not use is a shell script.
-
klor over 7 yearsYour solution was the closest one to what I wanted to do. Unfortunately it seems nano doesn't have capability to input text & create file non-interactively. I have to stick with echo "Hello World" > foo.txt kind of solution.
-
ValeriyKr over 7 yearsWhy do you want use
nano
in shell script? There are better ways in many cases, I think. -
klor over 7 yearsAlthough not a real solution, but you pointed out, that non interactive text input into nano is not possible, and gave proof about is. Accepting your answer as solution. Sticking with the usual echo "Hello World" > foo.txt kind solution.
-
klor over 7 yearsI will not use nano for this goal, as it seems doesn't have the built possibility to input non-interactive text.
-
Christian Meißner over 7 years@klor what is you intention to use nano i such a case? I don't understand you needs for this.