Bash substitution string example

Introduction

Bash, the popular shell scripting language in the Unix and Linux world, offers a variety of powerful tools for string manipulation. Among these tools, “Bash String Substitution” stands out as a crucial technique that allows you to easily modify and transform strings. Whether you are a software developer, system administrator, or a beginner learning Bash, mastering this technique can significantly boost your productivity and efficiency. In this article, we will explore the different methods of string substitution in Bash, from basic to advanced, to help you harness the full potential of this tool. The bash script is 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 terminal screen output:

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 terminal screen output:

Bash substitution string

Conclusion

Bash substitution string is a valuable and versatile technique that makes complex string manipulations straightforward. Mastering these substitution methods not only enables you to write more powerful and efficient scripts but also helps optimize your daily tasks.

We hope this article has provided you with a clear and comprehensive understanding of Bash String Substitution, equipping you to apply it effectively in your real-world projects. Keep exploring and experimenting to become a true expert in this field. I hope will this your helpful. For more details refer to Bash script.

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. 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.