In Bash we can pipe the output of one command to the next command. Here are some common examples:
Bash pipe output to next command
Suppose we have a command like:
echo -e "Python\nJava"
which create output like:
Python
Java
modify the output - awk
We can modify and pipe the output to next command like:
echo -e "Python\nJava" | awk '{print "Tested in "$1""}'
which modify the output of the first command to:
Tested in Python
Tested in Java
Another example of modifying output with awk
is extracting only several results from ls -l
command:
ls -l | awk '{print $1}'
This will print out only the first column of the output:
total
-rw-rw-r--
-rw-rw-r--
The full output of command ls -l
is:
total 164
-rw-rw-r-- 1 user user 0 Feb 17 16:54 data2.csv
-rw-rw-r-- 1 user user 232 Feb 20 00:34 files.txt
modify the output - sed
echo -e "Python\nJava" | sed 's/\(.*\)/Tested in \1/'
This chain two commands echo
and sed
to modify the output of the first one and produce new output:
Tested in Python
Tested in Java
Pass output as argument to next command
In bash we can pass the output of one command to the next one as an argument. We will cover multiple examples.
xargs
is very useful for passing information between commands when chaining is used in Bash:
echo -e "Python\nJava" | xargs -I{} echo 'test me in' {}
result:
test me in Python
test me in Java
Another example of passing output as argument with cURL:
cat urls.txt | xargs -I % curl http://example.com/%.json
we can read file, pass all paths to curl
command and then download the target files
For more info check:
Use pipes to pass output
In bash pipes ("|") are used to pass the output of one command as input to another command.
We can use the ls
command to list all the files in the current directory and then sort the output in alphabetical order:
ls | sort
result:
anaconda3
a.trace
cuda-workspace
data
Output redirection
We can redirect output from one command to another in Linux shells like:
Write
Operator >
will redirect the output of command to file. The operator will override the content of the file
ls -a > data.txt
The output of command ls -a
will be stored into file data.txt:
total 160
-rw-rw-r-- 1 user user 0 Feb 17 16:54 data.csv
-rw-rw-r-- 1 user user 0 Feb 20 00:34 files.txt
Append
To append output of command to file we can use >>
ls -a >> data.txt
command substitution
With command substitution we can replace command by it's output:
echo "$(ls | wc -l) files in the folder $(pwd)"
The result is execution of commands:
$(ls | wc -l)
$(pwd)
Which print this text on the console:
4 files in the folder /home/user/Downloads