Standard Input
Part of the Fundamentals section of Coddy's Terminal journey — lesson 40 of 82.
The second data stream is standard input, abbreviated as stdin. While standard output is where commands send their results, standard input is where commands receive data.
Many commands can read from a file by taking the filename as an argument:
cat notes.txt
sort names.txtBut you can also redirect a file's contents directly into a command using the < operator:
sort < names.txtThis feeds the contents of names.txt into the sort command through standard input. The result is the same as sort names.txt, but the mechanism is different — the shell handles the file reading instead of the command itself.
You can combine input and output redirection in a single command:
sort < names.txt > sorted_names.txtThis reads from names.txt, sorts the content, and writes the result to sorted_names.txt. Input redirection becomes especially useful when working with commands that expect data from standard input rather than a filename argument.
Challenge
EasyUse input redirection with the <code>< operator to sort the contents of numbers.txt numerically.
Run a single command that:
- Redirects
numbers.txtinto thesortcommand using <code>< - Uses the
-nflag to sort numerically
Hint: The format is
sort -n < filename. The shell reads the file and feeds its contents to the command through standard input.
Cheat sheet
Standard input (stdin) is where commands receive data.
You can redirect a file's contents into a command using the <code>< operator:
sort < names.txtThis feeds the file contents through standard input. The shell handles the file reading instead of the command itself.
You can combine input and output redirection:
sort < names.txt > sorted_names.txtThis reads from names.txt, sorts the content, and writes the result to sorted_names.txt.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline