109

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

7 Answers 7

347
word="tiger"
firstletter=${word:0:1}
1
  • 12
    Exactly what I was looking for. One-liner, without spawning subshells. Take my vote ! Jun 25, 2015 at 14:54
93
word=something
first=${word::1}
5
  • 1
    This would be wonderful, but does not work for me, neither in bash 4.3.11 nor in zsh 5.0.5
    – yoniLavi
    Mar 29, 2016 at 16:59
  • 1
    With GNU bash v4.3.11, first is set to the letter s. You can see it with echo $s. If that doesn't work for you, and you'd like help debugging, please copy and paste the command(s) you typed and their output.
    – Adam Liss
    Mar 30, 2016 at 18:54
  • 2
    Thanks Adam, my bad, I can't figure out why it didn't work for me before, but it works perfectly fine now on several version of bash I tried, going back to GNU bash, v3.1.0. Still doesn't work in zsh, but the question is tagged "bash", so that's irrelevant.
    – yoniLavi
    Mar 31, 2016 at 1:37
  • Thanks for following up and confirming! Glad it's working for you.
    – Adam Liss
    Apr 1, 2016 at 13:00
  • 1
    For zsh users: It is working with ${word:0:1}
    – luator
    Jun 10, 2021 at 12:07
46
initial="$(echo $word | head -c 1)"

Every time you say "first" in your problem description, head is a likely solution.

7
  • 8
    Please note that there is no -c option in POSIX head: <unix.com/man-page/posix/1/head>.
    – gioele
    Jan 9, 2015 at 10:02
  • 12
    This does return the first letter, but it's an overkill, and the solution depends on external utilities (head). This can be done in pure shell, which is cleaner.
    – automaciej
    Oct 7, 2016 at 9:51
  • 30
    This should not be the accepted answer, for the reasons pointed out by the above comments. The real answers are below - ${word:0:1} or ${word::1}.
    – noamtm
    Dec 8, 2016 at 7:37
  • 1
    For POSIX compliance you could do echo "$word" | fold -w1 | head -n 1, printf '%.1s' "$X" or printf '%c' "$X" but these are all subject to single v. multi byte character shenanigans.
    – phicr
    Mar 1, 2017 at 16:08
  • 1
    To update the accepted answer would take a @Villiage. Wanna? The community would thank you ...
    – erik258
    Dec 28, 2019 at 2:34
17

A portable way to do it is to use parameter expansion (which is a POSIX feature):

$ word='tiger'
$ echo "${word%"${word#?}"}"
t
2
  • More reasonable to use are the other answers with :: but I can't miss to upvote such an abuse of expansion. Nicely done. May 19, 2021 at 21:14
  • It's often the case that you need to shell out in a scripting environment that mandates, well POSIX sh, not bash (though that was not part of the question)
    – usretc
    Sep 7, 2021 at 19:57
7

With cut :

word='tiger'
echo "${word}" | cut -c 1
2

Since you have a sed tag here is a sed answer:

echo "$word" | sed -e "{ s/^\(.\).*/\1/ ; q }"

Play by play for those who enjoy those (I do!):

{

  • s: start a substitution routine
    • /: Start specifying what is to be substituted
    • ^\(.\): capture the first character in Group 1
    • .*:, make sure the rest of the line will be in the substitution
    • /: start specifying the replacement
    • \1: insert Group 1
    • /: The rest is discarded;
  • q: Quit sed so it won't repeat this block for other lines if there are any.

}

Well that was fun! :) You can also use grep and etc but if you're in bash the ${x:0:1} magick is still the better solution imo. (I spent like an hour trying to use POSIX variable expansion to do that but couldn't :( )

0
1

Using bash 4:

x="test"
read -N 1 var <<< "${x}"
echo "${var}"
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.