Give sort the contents of names.txt as standard input.
Do not use a filename operand for sort; use input redirection.
Which command is correct?
The < operator connects a file to a command's standard input (file descriptor 0). sort can read standard input when no file operand is provided.
Detailed explanation
sort > names.txtIncorrect. > sends sort's standard output to names.txt.
Incorrect. > sends sort's standard output to names.txt.
sort 2< names.txtIncorrect. 2< redirects standard error input, not standard input.
Incorrect. 2< redirects standard error input, not standard input.
sort < names.txtCorrect. It connects names.txt to sort's standard input.
Correct. It connects names.txt to sort's standard input.
names.txt | sortIncorrect. A filename is not a command that can feed a pipeline in this syntax.
Incorrect. A filename is not a command that can feed a pipeline in this syntax.
Try it yourself
An example you can run in a temporary verification environment.
printf 'bob
alice
' > /tmp/kp-names.txt; sort < /tmp/kp-names.txt; rm /tmp/kp-names.txtExpected result
alice
bobKey points
- < redirects standard input
- File descriptor 0 is standard input
- The output destination is unchanged
Notes
- Environment: Bash 5.2 / GNU coreutils 9.x
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.