Bash – Piping commands, modify stdin write to stdout

bashbufferstdinstdout

echo Hello World | nano - | less

I want to modify Hello -> Goodbye while in the the text editor.

It does not have to be with nano.
I am stuck with nano reading stdin but requiring me to write to a file (writing to – creates a file named -)

Best Answer

The moreutils package has a great command for doing this, called vipe. From the man page:

SYNOPSIS
       command1 | vipe | command2

DESCRIPTION
       vipe allows you to run your editor in the middle of a unix pipeline and
       edit the data that is being piped between programs. Your editor will have
       the full data being piped from command1 loaded into it, and when you close
       it, that data will be piped into command2.

By default this will use the editor command, which is usually just a symlink to the default command line editor. You can change this by either altering the link (use update-alternatives on Debian based systems) or using the EDITOR environmental variable. Eg, you could do:

echo Hello World | EDITOR=nano vipe | less

Otherwise, if the particular text editor doesn't have support for this kind of thing, I think you are stuck with manually creating a temporary file, writing the file to that, running the editor, inputting the file to the rest of the pipeline and removing the temporary file. The vipe command basically takes care of all this. This is nice, but the command is rarely available by default.

Related Question