Bash

From Grayhat cheatsheets
Jump to navigationJump to search

Bash (Bourne Again SHell) is the most common linux shell. Next are some tips for bash:

  • Bash has more features than sh, and some scripts written with bash in mind will give errors in sh, so better use bash by default to run scripts. For example, to read lines, and make sure that the interpreter doesn't separate each space as a new line, one needs to use bash's Internal Field Separator (IFS):

#!/bin/bash IFS=$'\n'     for i in `cat dns.txt`; do echo $i #|cut -d " " -f 1 done

With IFS we can force that different separators are considered, for example to separate in a for loop things separated by blank spaces, and that they appear as different iterations we could do: IFS=$' ' for i in 571 200 911; do echo $i; done

Ippsec does this in HackTheBox's Nineveh.

  • Blank spaces are important in bash. For example, it is correct to declare a variable like this:

a=5

but not like this: a = 5  

  • With wget we can download a complete website:

wget www.cisco.com

  • grep a b looks for the pattern "a" (specify with quotes for regular expressions) in file "b". grep -r a . looks for pattern "a" in all files in the current folder and subfolders.
  • cut -d x -f 4 is used to split a string (can be passed through a pipe) on each occurrence of the separator "x", and -f is used to select a particular occurrance. The equivalent in python would be a.split(x)[4]

 

  • sort -u orders and return unique occurrences of what is passed through a pipe.
  • Calculator for bash (needs to be installed): bc -ql

  

  • file x gives information of what type of file "x" is.
  • uniq -c counts the number of unique elements passed through a pipe.
  • wc is used for word count.