“Bash verificar si la cadena contiene subcadena” Código de respuesta

Bash si subcadena

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

Verificar si existe una subcadena en una fiesta de cadenas

string='Haystack';

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

prueba de subcadena de bash

#!/bin/bash

STR='GNU/Linux is an operating system'
SUB='Linux'

if grep -q "$SUB" <<< "$STR"; then
  echo "It's there"
fi
Wide-eyed Wren

Bash verificar si la cadena contiene subcadena

# 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
Charles-Alexandre Roy

prueba de subcadena de bash

#!/bin/bash

STR='GNU/Linux is an operating system'
SUB='Linux'

case $STR in

  *"$SUB"*)
    echo -n "It's there."
    ;;
esac
Wide-eyed Wren

Respuestas similares a “Bash verificar si la cadena contiene subcadena”

Preguntas similares a “Bash verificar si la cadena contiene subcadena”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código