DEV Community

Eduardo Issao Ito
Eduardo Issao Ito

Posted on

3

Split string in two parts with bash

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

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more