Shell features you didn't know you needed (or possibly even existed) #9

Published: (April 2, 2026 at 10:51 AM EDT)
1 min read
Source: Dev.to

Source: Dev.to

Replacing / in a Git branch name for Docker tags

When converting a Git branch name like // into a Docker image tag, the / characters must be removed or replaced because Docker tags cannot contain /.

Using tr

IMAGE_TAG=$(echo "$BRANCH" | tr '/' '-')

Using sed

IMAGE_TAG=$(echo "$BRANCH" | sed -e 's/\//-/g')

Using perl

IMAGE_TAG=$(echo "$BRANCH" | perl -pe 's/\//-/g')

Using Bash parameter expansion

IMAGE_TAG=${BRANCH//\//-}
  • ${VARIABLE/from/to} replaces the first occurrence of from with to.
  • ${VARIABLE//from/to} replaces all occurrences of from with to.
0 views
Back to Blog

Related posts

Read more »

Cx Dev Log — 2026-04-05

Overview Merging branches isn’t usually the most thrilling part of a project, but it’s crucial for keeping the bigger picture in sync. Today I focused on branc...