# How do I get the nth character from a string?

**URL:** <https://community.unix.com/t/how-do-i-get-the-nth-character-from-a-string/162714>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [June 22, 2006, 6:11am UTC](https://community.unix.com/t/how-do-i-get-the-nth-character-from-a-string/162714 "2006-06-22T06:11:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![toughman](https://community.unix.com/letter_avatar/toughman/32/6_22734026db12803ebb3e059622ac3534.png) [@toughman](https://community.unix.com/u/toughman)\
**Post date:** [June 22, 2006, 6:11am UTC](https://community.unix.com/t/how-do-i-get-the-nth-character-from-a-string/162714/1 "2006-06-22T06:11:05Z")

</div>

How to I get the nth character from a string in shell script. For instance, I have a string London. I want to get, say the first character (L) from the string. How do I do this in unix shell?

Thankx

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [June 22, 2006, 6:35am UTC](https://community.unix.com/t/how-do-i-get-the-nth-character-from-a-string/162714/2 "2006-06-22T06:35:04Z")

</div>

To get d From London

```nohighlight
echo "London" | head -c 4 | tail -c 1

```

You could also use the Bash specific construct. See this

```nohighlight
$ echo $JAVA_HOME
/usr/java/j2sdk1.4.2_07
$ echo ${JAVA_HOME:0:1}
/
$ echo ${JAVA_HOME:1:1}
u

```

---

<div class="post-metadata">

**Author:** ![Ygor](https://community.unix.com/user_avatar/community.unix.com/ygor/32/104_2.png) [@Ygor](https://community.unix.com/u/Ygor)\
**Post date:** [June 22, 2006, 8:00am UTC](https://community.unix.com/t/how-do-i-get-the-nth-character-from-a-string/162714/3 "2006-06-22T08:00:28Z")

</div>

Or..

```nohighlight
expr substr London 1 1

```

---

<div class="post-metadata">

**Author:** ![thestevew](https://community.unix.com/letter_avatar/thestevew/32/6_22734026db12803ebb3e059622ac3534.png) [@thestevew](https://community.unix.com/u/thestevew)\
**Post date:** [June 22, 2006, 9:45am UTC](https://community.unix.com/t/how-do-i-get-the-nth-character-from-a-string/162714/4 "2006-06-22T09:45:01Z")

</div>

Ksh93 has some extra string things, like:

```nohighlight
$ x=London
$ print ${x:3:1} ;# syntax is ${string:index:length}
n

```

cheers

---

<div class="post-metadata">

**Author:** ![bhargav](https://community.unix.com/user_avatar/community.unix.com/bhargav/32/130_2.png) [@bhargav](https://community.unix.com/u/bhargav)\
**Post date:** [June 22, 2006, 12:54pm UTC](https://community.unix.com/t/how-do-i-get-the-nth-character-from-a-string/162714/5 "2006-06-22T12:54:08Z")

</div>

> [@vino](#):
>
> To get d From London
> 
> ```plaintext
> echo "London" | head -c 4 | tail -c 1
> 
> ```

```plaintext
echo "London" | head -c 4 | tail -c 2

```
