mirror of
https://github.com/jbranchaud/til
synced 2026-01-07 09:08:01 +00:00
Add Partial String Matching In Bash Scripts as a unix til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_395 TILs and counting..._
|
_396 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -362,6 +362,7 @@ _395 TILs and counting..._
|
|||||||
- [Last Argument Of The Last Command](unix/last-argument-of-the-last-command.md)
|
- [Last Argument Of The Last Command](unix/last-argument-of-the-last-command.md)
|
||||||
- [List All Users](unix/list-all-users.md)
|
- [List All Users](unix/list-all-users.md)
|
||||||
- [Only Show The Matches](unix/only-show-the-matches.md)
|
- [Only Show The Matches](unix/only-show-the-matches.md)
|
||||||
|
- [Partial String Matching In Bash Scripts](unix/partial-string-matching-in-bash-scripts.md)
|
||||||
- [Repeat Yourself](unix/repeat-yourself.md)
|
- [Repeat Yourself](unix/repeat-yourself.md)
|
||||||
- [Saying Yes](unix/saying-yes.md)
|
- [Saying Yes](unix/saying-yes.md)
|
||||||
- [Search History](unix/search-history.md)
|
- [Search History](unix/search-history.md)
|
||||||
|
|||||||
23
unix/partial-string-matching-in-bash-scripts.md
Normal file
23
unix/partial-string-matching-in-bash-scripts.md
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# Partial String Matching In Bash Scripts
|
||||||
|
|
||||||
|
To compare two strings in a bash script, you will have a snippet of code
|
||||||
|
similar to the following:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
if [[ $(pwd) == "/path/to/current/directory" ]]
|
||||||
|
then
|
||||||
|
echo "You are in that directory";
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
You may only want to do a partial string match. For this, you can use the
|
||||||
|
`*` wildcard symbol.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
if [[ $(pwd) == *"directory"* ]]
|
||||||
|
then
|
||||||
|
echo "You are in that directory";
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
[source](http://stackoverflow.com/questions/229551/string-contains-in-bash)
|
||||||
Reference in New Issue
Block a user