88

How to retrieve the first 10 characters of a variable with Bash?

FOO="qwertzuiopasdfghjklyxcvbnm"

I need to get qwertzuiop.

codeforester
  • 39,467
  • 16
  • 112
  • 140
user2093552
  • 1,019
  • 1
  • 10
  • 12
  • 3
    That isn't correct assignment syntax. `foo=${foo:0:10}` ... `foo=${foo%%"${foo##??????????}"}` – ormaaj Feb 21 '13 at 00:44

2 Answers2

163

If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"

then

 echo ${FOO:0:10}

will give the first 10 characters.

P.P
  • 117,907
  • 20
  • 175
  • 238
81

Use the head command.

echo $FOO | head -c 10
=> qwertzuiop
pje
  • 21,801
  • 10
  • 54
  • 70
  • 1
    I think this answer make the most sense, and works at the end of a long line used to generate the string `FOO`. – Bryson S. Nov 02 '19 at 17:56