Tutorial by Examples

3.0 Check if a string consists in exactly 8 digits: $ date=20150624 $ [[ $date =~ ^[0-9]{8}$ ]] && echo "yes" || echo "no" yes $ date=hello $ [[ $date =~ ^[0-9]{8}$ ]] && echo "yes" || echo "no" no
Preparation $ mkdir globbing $ cd globbing $ mkdir -p folder/{sub,another}folder/content/deepfolder/ touch macy stacy tracy "file with space" folder/{sub,another}folder/content/deepfolder/file .hiddenfile $ shopt -u nullglob $ shopt -u failglob $ shopt -u dotglob $ shopt -u nocaseg...
4.0 Preparation $ mkdir globbing $ cd globbing $ mkdir -p folder/{sub,another}folder/content/deepfolder/ touch macy stacy tracy "file with space" folder/{sub,another}folder/content/deepfolder/file .hiddenfile $ shopt -u nullglob $ shopt -u failglob $ shopt -u dotglob $ shopt -u no...
Preparation $ mkdir globbing $ cd globbing $ mkdir -p folder/{sub,another}folder/content/deepfolder/ touch macy stacy tracy "file with space" folder/{sub,another}folder/content/deepfolder/file .hiddenfile $ shopt -u nullglob $ shopt -u failglob $ shopt -u dotglob $ shopt -u nocaseg...
Preparation $ mkdir globbing $ cd globbing $ mkdir -p folder/{sub,another}folder/content/deepfolder/ touch macy stacy tracy "file with space" folder/{sub,another}folder/content/deepfolder/file .hiddenfile $ shopt -u nullglob $ shopt -u failglob $ shopt -u dotglob $ shopt -u nocaseg...
Preparation $ mkdir globbing $ cd globbing $ mkdir -p folder/{sub,another}folder/content/deepfolder/ touch macy stacy tracy "file with space" folder/{sub,another}folder/content/deepfolder/file .hiddenfile $ shopt -u nullglob $ shopt -u failglob $ shopt -u dotglob $ shopt -u nocaseg...
Preparation $ mkdir globbing $ cd globbing $ mkdir -p folder/{sub,another}folder/content/deepfolder/ touch macy stacy tracy "file with space" folder/{sub,another}folder/content/deepfolder/file .hiddenfile $ shopt -u nullglob $ shopt -u failglob $ shopt -u dotglob $ shopt -u nocaseg...
Preparation $ mkdir globbing $ cd globbing $ mkdir -p folder/{sub,another}folder/content/deepfolder/ touch macy stacy tracy "file with space" folder/{sub,another}folder/content/deepfolder/file .hiddenfile $ shopt -u nullglob $ shopt -u failglob $ shopt -u dotglob $ shopt -u nocaseg...
2.02 Preparation $ mkdir globbing $ cd globbing $ mkdir -p folder/{sub,another}folder/content/deepfolder/ touch macy stacy tracy "file with space" folder/{sub,another}folder/content/deepfolder/file .hiddenfile $ shopt -u nullglob $ shopt -u failglob $ shopt -u dotglob $ shopt -u n...
pat='[^0-9]+([0-9]+)' s='I am a string with some digits 1024' [[ $s =~ $pat ]] # $pat must be unquoted echo "${BASH_REMATCH[0]}" echo "${BASH_REMATCH[1]}" Output: I am a string with some digits 1024 1024 Instead of assigning the regex to a variable ($pat) we could als...
a='I am a simple string with digits 1234' pat='(.*) ([0-9]+)' [[ "$a" =~ $pat ]] echo "${BASH_REMATCH[0]}" echo "${BASH_REMATCH[1]}" echo "${BASH_REMATCH[2]}" Output: I am a simple string with digits 1234 I am a simple string with digits 1234

Page 1 of 1