Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping in on the command line simulating a file?

I've seen the technique before, but don't know what it's called and forget the exact syntax. Let's say I need to pipe in a file to a program like: command < input-file. However, I want to directly pass these lines of the input file into the command without the intermediate input file. It looks something like this, but it doesn't work:

command < $(file-line1; file-line2; file-line3)

Can someone tell me what this is called and how to do it?

like image 855
user449511 Avatar asked May 02 '11 01:05

user449511


People also ask

How do you pipe a command line?

The | command is called a pipe. It is used to pipe, or transfer, the standard output from the command on its left into the standard input of the command on its right. # First, echo "Hello World" will send Hello World to the standard output.

How do I bash a pipe to a file?

To use bash redirection, you run a command, specify the > or >> operator, and then provide the path of a file you want the output redirected to. > redirects the output of a command to a file, replacing the existing contents of the file.

How do I redirect console output to a file?

Redirect Output to a File Only To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

Can you pipe in a bash script?

A pipe in Bash takes the standard output of one process and passes it as standard input into another process. Bash scripts support positional arguments that can be passed in at the command line.


2 Answers

This is called Process Substitution

command < <(printf "%s\n" "file-line1" "file-line2" "file-line3")

With the above, command will think its being input a file with a name much like /dev/fd/XX where 'XX' is some number. As you mentioned, this is a temporary file (actually a file descriptor) but it will contain the 3 lines you passed in to the printf command.

like image 69
SiegeX Avatar answered Oct 03 '22 11:10

SiegeX


Herestring.

command <<< $'line 1\nline 2\nline 3\n'

Or heredoc.

command << EOF
line 1
line 2
line 3
EOF
like image 20
Ignacio Vazquez-Abrams Avatar answered Oct 03 '22 11:10

Ignacio Vazquez-Abrams