Print A to Z according to user's input: 1=A, 2=B, etc [closed]
I want to print characters from A to Z depending on the user's input.
If the input is 1
then print A
, if the input is 2
then print B
, and so on.
I tried using a for
loop as below, but wasn't able to achieve what I want.
for i in {a..z}
do
echo $i
done
How can I use a loop or another approach to get the expected output?
Solution 1:
You can use an array:
#!/bin/bash
read i
a=(0 {A..Z})
echo ${a[$i]}
As arrays are zero-indexed, I simply put a 0
in front, so the array will be 0 A B C ...
. Otherwise you would get 0
→A
, 1
→B
, ...