Score:-1

Print A to Z according to user's input: 1=A, 2=B, etc

pr flag

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?

Score:3
in flag

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 0A, 1B, ...

kalpesh avatar
pr flag
thanks for your helps.
kalpesh avatar
pr flag
now i trying reverse means...input A =1 or B=2 and so on what should i do changes in given script
kalpesh avatar
pr flag
input A and expected output is 1
kalpesh avatar
pr flag
i tried this bot face error 'A+' : not a valid identifier. write full script
kalpesh avatar
pr flag
n={1..26} how to store this in array for access later by index
pLumo avatar
in flag
See the `(...)` around the `{...}`, that makes it an array.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.