# Extracting substring from variable

**URL:** <https://community.unix.com/t/extracting-substring-from-variable/360444>\
**Category:** Shell Programming and Scripting\
**Created:** [May 18, 2016, 10:24am UTC](https://community.unix.com/t/extracting-substring-from-variable/360444 "2016-05-18T10:24:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvindshukla81](https://community.unix.com/letter_avatar/arvindshukla81/32/6_449c533f586939b6e5f246632a46421f.png) [@arvindshukla81](https://community.unix.com/u/arvindshukla81)\
**Post date:** [May 18, 2016, 10:24am UTC](https://community.unix.com/t/extracting-substring-from-variable/360444/1 "2016-05-18T10:24:03Z")

</div>

Hi All,

I'm facing issue wherein I have 2 character string like 'CR' and 'DR' and I want to extract just 1st character but am unable to do it. I tried below options but they are returning me 2nd character only,

```nohighlight
var="CR"

```

```nohighlight
echo ${var:1}

```

returns just "R"

```nohighlight
echo ${var:0}

```

returns "CR"

```nohighlight
expr substr $var 1 1

```

doesn't work at my end it.

Any suggestions please.

Thanks,  
Arvind.

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [May 18, 2016, 10:26am UTC](https://community.unix.com/t/extracting-substring-from-variable/360444/2 "2016-05-18T10:26:03Z")

</div>

```nohighlight
${var:0:1}

```

---

<div class="post-metadata">

**Author:** ![rbatte1](https://community.unix.com/letter_avatar/rbatte1/32/6_449c533f586939b6e5f246632a46421f.png) [@rbatte1](https://community.unix.com/u/rbatte1)\
**Post date:** [May 18, 2016, 10:28am UTC](https://community.unix.com/t/extracting-substring-from-variable/360444/3 "2016-05-18T10:28:10Z")

</div>

What shell are you working in?

If it's ksh (and possibly bash too) you can do this sort of thing:-

```nohighlight
echo ${var%?}

```

It looks shocking, but it is slicing the variable up a little. It is called variable substitution and you should be able to get a fuller explanation if you search for that. It would probably not be best if I try to re-write existing documentation.

I hope that this helps,  
Robin

---

<div class="post-metadata">

**Author:** ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/6_449c533f586939b6e5f246632a46421f.png) [@shamrock](https://community.unix.com/u/shamrock)\
**Post date:** [May 18, 2016, 11:17am UTC](https://community.unix.com/t/extracting-substring-from-variable/360444/4 "2016-05-18T11:17:53Z")

</div>

The one below should work irrespective of shell version...

```nohighlight
export var="DR"
echo ${var%"${var#[A-Z]}"}
D

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [May 18, 2016, 11:46am UTC](https://community.unix.com/t/extracting-substring-from-variable/360444/5 "2016-05-18T11:46:00Z")

</div>

```nohighlight
typeset -L1 var

```

---

<div class="post-metadata">

**Author:** ![arvindshukla81](https://community.unix.com/letter_avatar/arvindshukla81/32/6_449c533f586939b6e5f246632a46421f.png) [@arvindshukla81](https://community.unix.com/u/arvindshukla81)\
**Post date:** [May 19, 2016, 3:37am UTC](https://community.unix.com/t/extracting-substring-from-variable/360444/6 "2016-05-19T03:37:49Z")

</div>

Thanks alot to all, all the solution mentioned worked.
