Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

bash if substring

string='Hi substring' #To check if string has "Mylong" substring do
if [[ $string == *"substring"* ]]; then
  echo "String has substring"
fi
Comment

checking if a substring exists in a string bash

string='Haystack';

if [[ $string =~ "Needle" ]]
then
   echo "It's there!"
fi
Comment

bash check for substring in string

[[ $haystack =~ "Needle" ]]
Comment

bash if substring in string

# Example usage:
FULLSTRING='Full string to search'
if [[ $FULLSTRING == *"ll str"* ]]; then
	echo "The substring 'll str' is in the full string."
fi

# Example to check for two substrings:
FULLSTRING='Full string to search'
if [[ $FULLSTRING == *"Full"* && $FULLSTRING == *"to"* ]]; then
	echo "The substrings 'Full' and 'to' are in the full string."
fi

# Note, see the following two links for why [[ ]] is used:
https://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash
http://mywiki.wooledge.org/BashFAQ/031
Comment

PREVIOUS NEXT
Code Example
Shell :: wordpress update core and plugins cli 
Shell :: run production environment nodejs 
Shell :: linux find installation location 
Shell :: git language 
Shell :: brew upgrade casks 
Shell :: extract tar.gz in linux 
Shell :: export display connection for wsl 
Shell :: ubuntu change username 
Shell :: tail -f linux last 100 lines 
Shell :: uniq bash 
Shell :: kubectl exec run command inside pod 
Shell :: git remote repository not found vs code 
Shell :: ssh transfer file 
Shell :: npm install xlsx 
Shell :: no matching manifest for linux/arm64/v8 in the manifest list entries 
Shell :: find exec rm 
Shell :: git global settings ssh 
Shell :: how to remove stuff from git 
Shell :: adb kill all emulators 
Shell :: git exclude file 
Shell :: set time in kali linux 
Shell :: wsl 
Shell :: git config credential.username 
Shell :: how to configure httpd in amazon ec2 instance 
Shell :: docker build with args 
Shell :: import csv into mongodb 
Shell :: bash cheat sheet 
Shell :: how to fast forward git 
Shell :: …or push an existing repository from the command line 
Shell :: ip route delete linux 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =