Horje
shell check if string contains substring Code Example
checking if a substring exists in a string bash
string='Haystack';

if [[ $string =~ "Needle" ]]
then
   echo "It's there!"
fi
shell check if string contains substring
# 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




Shell

Related
zsh: command not found: pg_restore docker Code Example zsh: command not found: pg_restore docker Code Example
bash move a list of files Code Example bash move a list of files Code Example
powershell connect to serial port Code Example powershell connect to serial port Code Example
how to set environment variable in linux permanently Code Example how to set environment variable in linux permanently Code Example
ubuntu set environment variable permanently Code Example ubuntu set environment variable permanently Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
11