First character of a variable in shell

Published: Sunday, Nov 30, 2008 Last modified: Friday, May 24, 2024

Bash Parameter Expansion is the expansion of a parameter: $foo, $1. You can use it to perform more complex operations: ${file%.mp3}, ${0##*/}. Remember you should almost always quote them: echo “$foo”. See

FOO="bar" && echo "${FOO:0:1}"

The POSIX way (recommended) is to use cut:

echo bar | cut -c 1