How to obtain the first letter in a Bash variable?

Village picture Village · Apr 18, 2012 · Viewed 80.1k times · Source

I have a Bash variable, $word, which is sometimes a word or sentence, e.g.:

word="tiger"

Or:

word="This is a sentence."

How can I make a new Bash variable which is equal to only the first letter found in the variable? E.g., the above would be:

echo $firstletter
t

Or:

echo $firstletter
T

Answer

Karoly Horvath picture Karoly Horvath · Apr 18, 2012
word="tiger"
firstletter=${word:0:1}