How to get the char at a given position of a string in shell script?


In bash with "Parameter Expansion" ${parameter:offset:length}

$ var=abcdef
$ echo ${var:0:1}
$ echo ${var:3:1}

The same parameter expansion can be used to assign a new variable:

$ x=${var:1:1}
$ echo $x

Edit: Without parameter expansion (not very elegant, but that's what came to me first)

$ charpos() { pos=$1;shift; echo "$@"|sed 's/^.\{'$pos'\}\(.\).*$/\1/';}
$ charpos 8 what ever here

Alternative to parameter expansion is expr substr

    substring of STRING, POS counted from 1

For example:

$ expr substr hello 2 1
  • cool, should have checked expr more thoroughly.
    – forcefsck
    Mar 17 '11 at 18:41
  • 2
    While this appears to work with the expr from GNU coreutils, substr is not included in the expr from FreeBSD, NetBSD or OS X. This isn't a portable solution.
    – ghoti
    Feb 14 '17 at 17:04
  • 1
    @ghoti, note that substr is not originally a GNU extension. The original implementation of expr came from PWB Unix in the late 70s and had substr (but not :). Sep 6 '19 at 8:06
  • @StéphaneChazelas, thanks for adding historical perspective. :) While I'm pretty sure PWB usage isn't relevant to the OP, it's always fun to track features and changes through the decades. GNU tends to be many people's default, but in general, I think I'd avoid using options that aren't clearly POSIX, and are known to be missing from major unices.
    – ghoti
    Sep 6 '19 at 20:47

cut -c

If the variable does not contain newlines you can do:

printf '%s\n' "$myvar" | cut -c2



awk substr is another POSIX alternative that works even if the variable has newlines:

myvar="$(printf 'a\nb\n')" # note that the last newline is stripped by
                           # the command substitution
awk -- 'BEGIN {print substr (ARGV[1], 3, 1)}' "$myvar"



printf '%s\n' is to avoid problems with escape characters: https://stackoverflow.com/a/40423558/895245 e.g.:

printf '%s\n' "$myvar" | cut -c1

outputs \ as expected.

See also: https://stackoverflow.com/questions/1405611/extracting-first-two-characters-of-a-string-shell-scripting

Tested in Ubuntu 19.04.

  • printf '%s' "$myvar" | cut -c2 is not POSIX as the output of printf is not text unless $myvar ends in a newline character. It otherwise assumes the variable doesn't contain newline characters as cut cuts each line of its input. Sep 6 '19 at 8:11
  • The awk one would be more efficient and reliable with awk -- 'BEGIN {print substr (ARGV[1], 2, 1)}' "$myvar" Sep 6 '19 at 8:11
  • Note that with current versions of GNU cut, that doesn't work for multi-byte characters (same for mawk or busybox awk) Sep 6 '19 at 8:12
  • @StéphaneChazelas thanks for the points. I didn't understand what you mean in the first one: do you mean that printf 'abc '| cut -c2 is wrong because no \n (this I don't know about), or that the command will fail if myvar has newlines (this I agree)? Sep 6 '19 at 8:18
  • 1
    The behaviour of cut is unspecified if the input is not text (though cut implementations are required to handle lines or arbitrary length). The output of printf abc is not text as it doesn't end in a newline character. In practice depending on the implementation, if you pipe that to cut -c2, you get either b, b<newline> or nothing at all. You'd need printf 'abc\n' | cut -c2 to get a behaviour specified by POSIX (that's required to output b<newline>) Sep 6 '19 at 11:00

With zsh or yash, you'd use:

$ text='€$*₭£'
$ printf '%s\n' "${text[3]}"

(in zsh, you can shorten it to printf '%s\n' $text[3]).


You can use the cut command. To get the 3rd postion:

echo "SAMPLETEXT" | cut -c3

Check this link http://www.folkstalk.com/2012/02/cut-command-in-unix-linux-examples.html

(Advanced cases) However, modifying IFS is also a good thing, especially when your input might have spaces. In that case alone, use the one below

IFS=$(echo -en "\n\b")
echo "SAMPLETEXT" | cut -c3
  • 1
    I can't see how IFS would come into play in the code that you have posted.
    – Kusalananda
    Aug 7 '18 at 7:01

shell cut - print specific range of characters or given part from a string

#method1) using bash

 echo ${str:11:8}

#method2) using cut

 cut -c12-19 <<< $str

#method3) when working with awk

 awk '{time=gensub(/.{11}(.{8}).*/,"\\1","g",$1); print time}' <<< $str

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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