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?
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.
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.
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.
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.
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.
Herestring.
command <<< $'line 1\nline 2\nline 3\n'
Or heredoc.
command << EOF
line 1
line 2
line 3
EOF
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With