Bash string ends with

 

We can check if a string ends with specific word/string.

Lets try to validate with an example.

Here I am trying to check if a string ends with def given my string is abcdef.

S="abcdef"
if [[ "$S" == *def ]]
then
    echo true
else
    echo false
fi

Write above mentioned code in a script, here I have written it in tmp.sh file and run script with following command:

$ bash tm.sh

Output:

true

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s