Input redirection in pipes

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

← How to use pipes to connect programsHomeOutput redirection in pipes →

Normally, when you pipe commands, the standard output (STDOUT) of one command becomes the standard input (STDIN) for the next. Input redirection with pipes allows you to send a file's contents or the output of another process as input instead of relying on the prior STDOUT.

Syntax

command1 < input.txt | command2
command1 < input.txt | command2 arg1 | command3

Examples

For example, the sort command will get input from /etc/passwd file, which is piped to grep command:

sort < input.txt | grep something
sort < input.txt | uniq | grep something

Here Documents (With <<)

The syntax is

command << EOF
This is line one
This is line two
EOF

Example:

#!/bin/bash
t="$1"
IFS="."
set -- $t
s="${1}.${2}.${3}.$(sed -e 's/32/24/' -e 's/^[0-9]\//0\//' <<<"$4")"
cat <<EOF
#!/bin/sh
/sbin/ip link set \$INTERFACE up
/sbin/ip addr add  $t dev \$INTERFACE
/sbin/ip route add $s dev \$INTERFACE

#!/bin/sh
/sbin/ip route del $s dev \$INTERFACE
/sbin/ip addr del $t dev \$INTERFACE
/sbin/ip link set \$INTERFACE down

EOF


← How to use pipes to connect programsHomeOutput redirection in pipes →