Issue
How can i shift each letter of a string by a given number of letters down or up in bash, without using a hardcoded dictionary?
Solution
If you mean something like ROT13, you can use tr
:
pax$ echo 'hello there' | tr 'a-z' 'n-za-m'
uryyb gurer
pax$ echo 'hello there' | tr 'a-z' 'n-za-m' | tr 'a-z' 'n-za-m'
hello there
For a more general solution where you want to provide an arbitrary rotation (0 through 26), you can use:
#!/usr/bin/bash
dual=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
phrase='hello there'
rotat=13
newphrase=$(echo $phrase | tr "${dual:0:26}" "${dual:${rotat}:26}")
echo ${newphrase}
Answered By - paxdiablo Answer Checked By - Willingham (WPSolving Volunteer)