How to retrieve the first n characters of a variable in Bash?

Bash

Bash Problem Overview


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

FOO="qwertzuiopasdfghjklyxcvbnm"

I need to get qwertzuiop.

Bash Solutions


Solution 1 - Bash

If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"

then

 echo ${FOO:0:10}

will give the first 10 characters.

Solution 2 - Bash

Use the head command.

echo $FOO | head -c 10
=> qwertzuiop

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2093552View Question on Stackoverflow
Solution 1 - BashP.PView Answer on Stackoverflow
Solution 2 - BashpjeView Answer on Stackoverflow