How to obtain the first letter in a Bash variable?
Asked Answered
G

7

115

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
Goldsmith answered 18/4, 2012 at 21:42 Comment(0)
N
46
initial="$(echo $word | head -c 1)"

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

Nicky answered 18/4, 2012 at 21:44 Comment(7)
Please note that there is no -c option in POSIX head: <unix.com/man-page/posix/1/head>.Dirge
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.Gorcock
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}.Radiator
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.Alchemist
Wait, so is head -c n, whoops. echo "fábio" | head -c 2 gives f� , so that's another problem with this I guess.Alchemist
To update the accepted answer would take a @Villiage. Wanna? The community would thank you ...Sherris
This is overkill if you want to get the first character(s) of a bash variable. It is a fine solution if you want the first character(s) of anything else regardless of your shell. E.g. in a script run by sh: date +%N | head -c 7.Newlywed
E
353
word="tiger"
firstletter=${word:0:1}
Edna answered 18/4, 2012 at 21:46 Comment(1)
Exactly what I was looking for. One-liner, without spawning subshells. Take my vote !Karlotte
K
92
word=something
first=${word::1}
Kassel answered 18/4, 2012 at 22:3 Comment(5)
This would be wonderful, but does not work for me, neither in bash 4.3.11 nor in zsh 5.0.5Echovirus
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.Kassel
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.Echovirus
Thanks for following up and confirming! Glad it's working for you.Kassel
For zsh users: It is working with ${word:0:1}Marnie
N
46
initial="$(echo $word | head -c 1)"

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

Nicky answered 18/4, 2012 at 21:44 Comment(7)
Please note that there is no -c option in POSIX head: <unix.com/man-page/posix/1/head>.Dirge
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.Gorcock
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}.Radiator
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.Alchemist
Wait, so is head -c n, whoops. echo "fábio" | head -c 2 gives f� , so that's another problem with this I guess.Alchemist
To update the accepted answer would take a @Villiage. Wanna? The community would thank you ...Sherris
This is overkill if you want to get the first character(s) of a bash variable. It is a fine solution if you want the first character(s) of anything else regardless of your shell. E.g. in a script run by sh: date +%N | head -c 7.Newlywed
O
18

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

$ word='tiger'
$ echo "${word%"${word#?}"}"
t
Osmosis answered 8/7, 2017 at 16:45 Comment(2)
More reasonable to use are the other answers with :: but I can't miss to upvote such an abuse of expansion. Nicely done.Pet
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)Coburg
O
7

With cut :

word='tiger'
echo "${word}" | cut -c 1
Oglethorpe answered 30/8, 2018 at 14:59 Comment(0)
A
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 :( )

Alchemist answered 1/3, 2017 at 18:0 Comment(0)
D
1

Using bash 4:

x="test"
read -N 1 var <<< "${x}"
echo "${var}"
Dysfunction answered 22/10, 2017 at 5:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.