values.txt contains 2, 10, and 100 on separate lines.
Sort them by numeric value in ascending order rather than lexicographically.
Which command is appropriate?
sort -n compares lines as numbers. Add -r when reverse numeric order is needed.
Detailed explanation
sort -n values.txtCorrect. sort -n compares the values numerically.
Correct. sort -n compares the values numerically.
sort values.txtIncorrect. Default sort uses string comparison, which can place 10 before 2.
Incorrect. Default sort uses string comparison, which can place 10 before 2.
sort -r values.txtIncorrect. -r reverses the comparison but does not enable numeric comparison.
Incorrect. -r reverses the comparison but does not enable numeric comparison.
uniq values.txtIncorrect. uniq removes adjacent duplicates and does not sort values.
Incorrect. uniq removes adjacent duplicates and does not sort values.
Try it yourself
An example you can run in a temporary verification environment.
printf '10\n2\n100\n' | sort -nExpected result
2
10
100Key points
- -n means numeric
- Default sort is lexical
- -r reverses the result
Notes
- Environment: 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.