Skip to main content

BASH ULTIMATE MAN PAGE

====================================================================================

  #!/bin/bash

Beginning a bash script

====================================================================================

echo 'enter domain name to test'
read domain

Set user input as a variable

====================================================================================

GREP

-----------------------------------------------------------------------------------------------------------------------------------------------

grep -i pattern filename

ignore character case

-----------------------------------------------------------------------------------------------------------------------------------------------

grep -v pattern filename

inverted match - essentially removes whatever pattern is specified.

-----------------------------------------------------------------------------------------------------------------------------------------------

grep -H pattern filenames

 

Print the file name for each match - useful for grepping patterns in multiple files. To achieve the opposite (no files names) pass -h.

-----------------------------------------------------------------------------------------------------------------------------------------------

grep -n pattern filename

Prefix each line of output with the 1-based line number within its input file.

-----------------------------------------------------------------------------------------------------------------------------------------------

   Character Classes and Bracket Expressions

A bracket expression is a list of characters enclosed by [ and ].  It matches any single character in that list.  If the first character of the list is the caret ^ then it matches any character not in the list; it is unspecified whether it matches an encoding error.  For example, the regular expression [0123456789] matches any single digit.

   Basic vs Extended Regular Expressions
       In basic regular expressions the meta-characters ?, +, {, |, (,
       and ) lose their special meaning; instead use the backslashed
       versions \?, \+, \{, \|, \(, and \).

   Repetition
       A regular expression may be followed by one of several repetition
       operators:
       ?      The preceding item is optional and matched at most once.
       *      The preceding item will be matched zero or more times.
       +      The preceding item will be matched one or more times.
       {n}    The preceding item is matched exactly n times.
       {n,}   The preceding item is matched n or more times.
       {,m}   The preceding item is matched at most m times.  This is a
              GNU extension.
       {n,m}  The preceding item is matched at least n times, but not
              more than m times.

 

====================================================================================

ECHO

echo -e

allow interpretation of backslash-escaped character:

      \a    alert (bell)
      \b    backspace
      \c    suppress further output
      \e    escape character
      \E    escape character
      \f    form feed
      \n    new line
      \r    carriage return
      \t    horizontal tab
      \v    vertical tab
      \\    backslash
      \0nnn    the character whose ASCII code is NNN (octal).  NNN can be
            0 to 3 octal digits
      \xHH    the eight-bit character whose value is HH (hexadecimal).  HH
            can be one or two hex digits
      \uHHHH    the Unicode character whose value is the hexadecimal value HHHH.
            HHHH can be one to four hex digits.
      \UHHHHHHHH the Unicode character whose value is the hexadecimal value
            HHHHHHHH. HHHHHHHH can be one to eight hex digits.

-----------------------------------------------------------------------------------------------------------------------------------------------

echo "${x%Y}"

Remove a character (Y) from a string

====================================================================================

SORT

sort -k1 

Sort a file or output by a specific column, ie sort -k1 would sort by the first column

------------------------------------------------------------------------------------------------------------------------------------------------

sort -n

Numeric sort (default sort order is ascending).

-----------------------------------------------------------------------------------------------------------------------------------------------

sort -r

Reverse the output of the sort.

====================================================================================

AWK

awk '{print $1,$2}'

Print the first and second column

------------------------------------------------------------------------------------------------------------------------------------------------