DEV Community

loading...

Split string in two parts with bash

Eduardo Issao Ito
Software architect, developer, learner.
・1 min read

We can use variable substitution to split a string using a character as separator:

STRING="x@y"

FIRST="${STRING%@*}"
SECOND="${STRING#*@}"

echo $STRING
echo $FIRST
echo $SECOND

Result:

x@y
x
y

Discussion (0)