How to input text into a new text file using nano from command line?

7,000

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.

Share:
7,000

Related videos on Youtube

klor
Author by

klor

Updated on September 18, 2022

Comments

  • klor
    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:

    1. nano is not capable of handling non-interactive text input.
    2. 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
      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
      JdeBP over 7 years
      Do not put answers in questions. That's not how this is supposed to work.
  • klor
    klor over 7 years
    Sorry, but your solution is not done completely from command line. Thus I can not use is a shell script.
  • klor
    klor over 7 years
    Sorry, but your solution is not done completely from command line. Thus I can not use is a shell script.
  • klor
    klor over 7 years
    Your 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
    ValeriyKr over 7 years
    Why do you want use nano in shell script? There are better ways in many cases, I think.
  • klor
    klor over 7 years
    Although 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
    klor over 7 years
    I will not use nano for this goal, as it seems doesn't have the built possibility to input non-interactive text.
  • Christian Meißner
    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.