Bash substitution string example

In this tutorial, How to use Bash substitution string. I usually use bash substitution. Bash script the essential for DevOps Roles.

Bash substitution string

From the beginning #
Remove the first matching pattern from the beginning of the string

#!/bin/bash

FILE="/usr/local/www/DevopsRoles.com/foo.txt"
echo "Path File: $FILE"

echo ${FILE#*.}
echo ${FILE#*/}
echo ${FILE#*m}

The screen output terminal:

Bash substitution string

Remove the last matching pattern from the beginning of the string

#!/bin/bash

FILE="/usr/local/www/DevopsRoles.com/foo.txt"
echo "Path File: $FILE"

echo ${FILE##*.}
echo ${FILE##*/}
echo ${FILE##*m}

The screen output terminal:

Bash substitution string

From the end equal %
Remove the first matching pattern from the end of the string

#!/bin/bash

FILE="/usr/local/www/DevopsRoles.com/foo.txt"
echo "Path File: $FILE"

echo ${FILE%.*}
echo ${FILE%/*}
echo ${FILE%m*}

The screen output terminal:

Bash substitution string

Remove the last matching pattern from the end of the string

#!/bin/bash

FILE="/usr/local/www/DevopsRoles.com/foo.txt"
echo "Path File: $FILE"

echo ${FILE%%.*}
echo ${FILE%%/*}
echo ${FILE%%m*}

The screen output terminal:

Bash substitution string

Conclusion

Thought the article, you can use Bash substitution string as above. I hope will this your helpful. More details refer to Bash script.

, ,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.