Category | Syntax / Command | Description |
---|---|---|
Text Manipulation | echo "Hello, World!" | Prints text to the terminal. |
Text Manipulation | printf "Name: %s\n" "Alice" | Formatted output. |
Arithmetic | expr 5 + 3 | Basic arithmetic. |
Variables | VAR="value" | Assigns a value to a variable. |
Loops | for i in {1..5}; do echo $i; done | For loop. |
Control Flow | if [ "$x" -eq 10 ]; then echo "Yes"; fi | If statement. |
Functions | my_func() { echo "Hello"; } | Defines a function. |
File Operations | touch file.txt | Creates an empty file. |
Process Management | ps aux | Lists running processes. |
Redirections | command > file.txt | Redirects output to a file. |
Pipes | command1 | command2 | Pipes output of one command to another. |
Pattern | Description | Example |
---|---|---|
. | Matches any single character (except newline). | /a.c/ matches "abc", "adc", "a#c" |
\d | Matches any digit (0-9). | /\d+/ matches "123", "42" |
\D | Matches any non-digit. | /\D+/ matches "abc", "hello" |
\w | Matches any word character (a-z, A-Z, 0-9, _). | /\w+/ matches "hello_123" |
\W | Matches any non-word character. | /\W+/ matches "!!!", "@@@" |
\s | Matches any whitespace (space, tab, newline). | /\s+/ matches spaces |
\S | Matches any non-whitespace character. | /\S+/ matches "word" |
^ | Matches the start of a line. | /^abc/ matches "abc" at start |
$ | Matches the end of a line. | /abc$/ matches "abc" at end |
[abc] | Matches any character inside brackets. | /[aeiou]/ matches vowels |
[^abc] | Matches any character NOT in brackets. | /[^0-9]/ matches non-digits |
(...) | Capturing group. | /(hello)/ captures "hello" |
| | Logical OR. | /cat|dog/ matches "cat" or "dog" |
* | Matches 0 or more occurrences. | /a*/ matches "", "a", "aaa" |
+ | Matches 1 or more occurrences. | /a+/ matches "a", "aaa" |
? | Matches 0 or 1 occurrences. | /a?/ matches "", "a" |
{n} | Matches exactly n times. | /a{3}/ matches "aaa" |
{n,} | Matches n or more times. | /a{2,}/ matches "aa", "aaa" |
{n,m} | Matches between n and m times. | /a{2,4}/ matches "aa", "aaa", "aaaa" |
Regex | Description | Example |
---|---|---|
. | Matches any single character except a newline (\n) | h.t matches hat, hit, but not h/t. |
^ | Anchors to the start of a string or line | ^Hello matches "Hello" at the beginning of a string. |
$ | Anchors to the end of a string or line | world$ matches "world" at the end of a string. |
\ | Escape character for special symbols | \. matches a literal . |
[] | Character class, matches any single character within | [aeiou] matches any vowel. |
[^] | Negated character class, matches any single character not within | [^aeiou] matches any non-vowel. |
| | Alternation (logical OR) | |
() | Capturing group; groups patterns and captures the match for later reference | (ab)+ matches "ab", "abab", etc. |
(?: ) | Non-capturing group; groups patterns without capturing them | (?:ab)+ matches "abab" but doesn’t capture it. |
{} | Quantifier specifying exact or range repetitions | a{3} matches "aaa". a{2,4} matches "aa", "aaa", or "aaaa". |
* | Matches 0 or more of the preceding character or group | a* matches "", "a", "aa", etc. |
+ | Matches 1 or more of the preceding character or group | a+ matches "a", "aa", etc., but not "". |
? | Matches 0 or 1 of the preceding character or group; makes quantifiers lazy when placed after | a? matches "", or "a". a*? matches as few "a"s as possible. |
/ | Delimiter for regex in some languages like JavaScript or Perl | /hello/ matches "hello". |
\d | Matches any digit (0-9) | \d{2} matches "12". |
\D | Matches any non-digit | \D matches "a", but not "2". |
\w | Matches any word character (alphanumeric + underscore) | \w+ matches "word123". |
\W | Matches any non-word character | \W matches "@", but not "a". |
\s | Matches any whitespace character (spaces, tabs, newlines) | \s+ matches spaces between words. |
\S | Matches any non-whitespace character | \S+ matches "word123", but not " ". |
\b | Matches a word boundary | \bword\b matches "word", but not "sword". |
\B | Matches a position that is not a word boundary | \Bword matches "sword", but not " word". |