Question : Find a pattern in a variable

Can anyone tell me how to find a pattern in a string of text in a variable using Bash in Linux?

Example:
var=Line 1: This is a line

I want to be able to search var to determine if : is part of the text.  If var does contain ":", I would like to be able to split it so that I can assign Line 1 to var1 and This is a line to var2.

Answer : Find a pattern in a variable

If you have bash, you might as well use it rather than call external utilities.
1:
2:
3:
4:
5:
6:
7:
8:
#!/bin/bash
var='Line 1: This is a line'
 
if echo $var | grep -q  :
then
   var1=${var%%:*}
   var2=${var#*:}
fi
Open in New Window Select All
Random Solutions  
 
programming4us programming4us