2021年3月27日 星期六

[Linux 常見問題] Bash - How to check if a string contains a substring in Bash

 Source From Here

Question
I have a string in Bash:
  1. string="My string"  
How can I test if it contains another string?

HowTo
You can use Marcus's answer (* wildcards) outside a case statement, too, if you use double brackets:
  1. string='My long string'  
  2. if [[ $string == *"My long"* ]]; then  
  3.   echo "It's there!"  
  4. fi  
Note that spaces in the needle string need to be placed between double quotes, and the * wildcards should be outside. Also note that a simple comparison operator is used (i.e. ==), not the regex operator =~.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...