I want to use the tr command to map chars to new chars, for example:
echo "hello" | tr '[a-z]' '[b-za-b]'
Will output:ifmmp
(where each letter in the lower-case alphabet is shifted over one to the right)
See below the mapping to new chars for '[b-za-b]'
:
[a b c d e f g h i j k l m n o p q r s t u v w x y z]
will map to:
[b c d e f g h i j k l m n o p q r s t u v w x y z a]
However, when I want it to rotate multiple times, how can I use a variable to control the rotate-value for the tr command?
Eg: for a shift of 1:
echo "hello" | tr '[a-z]' '[b-za-b]'
without variables and:
echo "hello" | tr '[a-z]' '[(a+$var)-za-(a+$var)]'
where $var=1here I have:
(a+$var)-z
representing the same asb-z
and....................
a-(a+$var)
representing the same asa-b
I have tried converting the ascii value to a char to use within the tr command but I don't think that is allowed.
My problem is that bash is not interpreting:
(a+$var)
as the char b
when $var=1
(a+$var)
as the char c
when $var=2
... etc.
How can I tell bash to interpret these equations as chars for the tr command
EDIT
I tried doing it with an array but it's not working:
chars=( {a..z} )
var=2
echo "hello" | tr '[a-z]' '[(${chars[var]}-z)(a-${chars[var]})]'
I used: (${chars[var]}-z)
to represent b-z
where var=1
Because ${chars[1]}
is b
but this is not working. Am I missing something?