Quantcast
Channel: shell – IO Digital Sec
Viewing all articles
Browse latest Browse all 18

BASH Shell Scripting – Sort a string alphabetically

$
0
0

I was asked today how to sort a string alphabetically with BASH

Using perl, you can easily enough use

print (join “”, sort split //,$_)

With bash however, the best option is:

echo “teststring” | grep -o . | sort -n |tr -d ‘n'; echo
Which returns: eginrssttt

A good way of enumerating each character from a string in general is:

for (( i = 0; i < ${#str[@]}; i++ )); do echo “${str[$i]}”; done


Viewing all articles
Browse latest Browse all 18

Latest Images

Trending Articles





Latest Images