BASH from beginner to advanced: Solutions to Hackerrank Challenges - Episode 18: Sort Command

Опубликовано: 24 Март 2026
на канале: Adam Djellouli
176
2

Welcome to this comprehensive series of videos where we will be giving solutions and explanations for various BASH challenges from Hackerrank. In this video, we will be addressing 7 tasks, including "Sort Command #1" (https://www.hackerrank.com/challenges..., "Sort Command #2" (https://www.hackerrank.com/challenges..., "Sort Command #3" (https://www.hackerrank.com/challenges..., "Sort Command #4" (https://www.hackerrank.com/challenges..., "Sort Command #5" (https://www.hackerrank.com/challenges..., "Sort Command #6" (https://www.hackerrank.com/challenges..., "Sort Command #7" (https://www.hackerrank.com/challenges....

Our goal in this series is to document my thought process and solutions, and to focus on concepts and ideas rather than details. I will also be highlighting the differences between BASH and popular programming languages like Python and JavaScript.

BASH is a powerful command-line interface for interacting with an operating system. It's a shell, and the most popular one. It can be used directly in the terminal or in scripts, which are text files containing a series of instructions. BASH is particularly useful for tasks such as text processing, file operations, connecting multiple programs, and system maintenance. It is also portable, working by default on many platforms.

The sort command in Unix-like operating systems is used to sort the lines of a file or the output of a command in a specified order. By default, sort sorts the lines in ascending order and prints the result to the standard output.

The basic syntax of the sort command is as follows:

sort [options] [file]

Here are some of the most commonly used options:

-r: This option sorts the lines in descending order.
-n: This option sorts the lines numerically.
-f: This option ignores case distinctions.
-u: This option removes duplicate lines.

If a file is specified, sort reads the file and sorts its lines. If no file is specified, sort reads from standard input.

Here's an example usage of the sort command:

$ cat example.txt
4
2
3
1
$ sort example.txt
1
2
3
4
$ sort -r example.txt
4
3
2
1
$ sort -n example.txt
1
2
3
4

In the above example, sort sorts the lines of the input file example.txt in ascending order by default, but it also can sort in descending order using the -r option, sort numerically using the -n option, or ignore case distinctions using the -f option. If the input file contains duplicate lines, the -u option can be used to remove them from the output.


If you want to see all the solutions for these challenges, you can find them on my Github repository: https://github.com/djeada/Bash-Hacker...

Also, you can find me on my website: https://adamdjellouli.com/ where I write about different programming topics, and you can see my other projects.

Don't forget to like and subscribe for more BASH tutorials and solutions to Hackerrank challenges.