Linux: Bash String Ends With...
In this article, we are going to check and see if a bash string ends with a specific word or string. This is quite a crucial action for most advanced users. Also, at the end of this post, we will share how you can access Linux partitions and view your Linux files on a Windows PC.
Here you will find out:
- how to check if a bash string ends with a specific word
- how to define the last character in bash strings
- when DiskInternals can help you
Are you ready? Let's read!
How to check if a bash string ends with a specific word
Using the example below, we will try to validate if a bash string ends with a specific word/string. The string is “testing”, and we are trying to ascertain if the string ends with “ing”.
You can copy the code above and save it as “test.sh”, then use the following command to run the script:
The output will be:
How to define the last character
You can use the command to find stings in another string. If you have a string that does not end with “/”, you can use grep to get its last character. The example below shows how to achieve this.
From the code above, “$?” denotes the exit status, and the –q flag is needed in case the argument turns true, and nothing is printed. If the match was found, the exit status would be 0. The –E flag allows regex matching, while the "/$" represents the end of the string.
Different ways of using regex match operators
There are quite different ways of using the regex match operator (=~), and here are the most common ways.
The following syntax is what to use to check and see if a string begins with a word or character.
Note: The most recent versions of bash (v3+) support the regex comparison operator “=~”.
To match this or that in a regex, use “|”.
user* means “use” and zero-or-more occurrences of “r”; thus, “use” and “userrr” is supported.
user.* means “user” and zero-or-more occurrences of any character; thus, “user1” or “userX” is supported.
^user.* means to match the pattern user.* at the beginning of $HOST.
Note: the expression above is a “proper” regular expression syntax.
The syntax below will check to see if $var starts with the hashtag:
To see if a string contains a substring, use the following syntax:
Note: the full stop (.) that comes before the asterisk (*) matches zero or more occurrences of any character that is not a newline character.
Finally, it is important to note that the regex match operator “=~” won’t work when you use single square brackets; you must double the square brackets.
So, is it possible to open Linux files from Windows or not?
Of course, it is very possible to open Linux files on Windows.
All you need is DiskInternals Linux Reader, a freeware app with a distinctive interface that is very easy to understand and navigate. DiskInternals Linux Reader works perfectly for users on a dual-boot PC or virtual machine. With the help of this software, you can easily access Linux partitions on Windows PC and view the files that are saved inside.