← Back to Bash Course | Chapter 11: Process Management | Lesson 7 of 12

sort

In this page:

  1. sort

sort

sort orders lines of text, alphabetically by default, or numerically with -n, and in reverse with -r. -k sorts by a specific field instead of the whole line. It's frequently the first step in a pipeline before deduplicating or summarizing data.

Note: Always add -n when sorting numbers as text sorts them lexically, putting "10" before "2".

Example: sort

bash
#!/bin/bash
printf "10\n2\n33\n4\n" | sort
echo "---"
printf "10\n2\n33\n4\n" | sort -n

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.