Give sort the contents of unsorted.txt through standard input.
Do not pass the filename as sort's ordinary argument.
Which command is appropriate?
< FILE opens FILE for reading and connects it to file descriptor 0, standard input.
Detailed explanation
sort > unsorted.txtIncorrect. > redirects sort's stdout and would overwrite the file.
sort >> unsorted.txtIncorrect. >> appends sort's stdout to the file.
sort < unsorted.txtCorrect. sort < unsorted.txt connects the file to standard input.
sort 2< unsorted.txtIncorrect. 2< operates on file descriptor 2, not normal standard input.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp); printf 'b\na\n' >"$tmp"; sort <"$tmp"; rm -f "$tmp"Expected result
a
bKey points
- File descriptor 0 is stdin
- < redirects input
- The source file is not modified
Notes
- Environment: Bash 5.x / GNU sort / 一時ファイル
- Use temporary files and harmless commands for stream experiments.
- Shell redirection syntax and xargs behavior should be checked on the target shell implementation.
Foundation review
Place the operation
Input redirection lets a command that normally reads a terminal consume a file instead.
Check the side effect
Compare redirected input with equivalent piped input using a harmless reader.