What is tee command in bash?
Overview
The tee command reads the standard input and simultaneously writes to a standard output, into the file(s).
Syntax
command | tee options filenames
Parameters
It takes the following parameters:
options:-
-a: Appends to the file instead of overriding it. --help: Displays the help message and exits.--version: Displays the version and exits.filename(s): Single or multiple file names separated by a space.
Let's take a look at an example.
Example
echo "List of files before executing the tee command"#list files in present directorylsecho "Creating the how_to_ls text file with the tee command"#create a text file with the tee commandman ls | tee how_to_ls.txtecho "List of files after executing the tee command"#list files in present directoryls
Explanation
In the above code snippet:
- Line 3: We print the list of files in the present directory before using the
teecommand with thelscommand. - Line 7: We display the output of the command
man lsand write it into a filehow_to_ls.txtusing theteecommand. - Line 11: We print the list of files in the present directory after executing the
teecommand with thelscommand. We can see in the output,how_to_ls.txtis created.