Skip to content

Introduction to Bash, VIM & Regex

Frontendmasters course on command line by James Halliday.

Written by Eva Dee on (about a 4 minute read).

Navigation permalink

bash is a unix (an operating system that evolved into Linux) shell. A shell is a computer program that is used to launch other computer programs ( via a text interface). bash comes from 'bourne again shell'.

  • ls -1

display directory content, line by line.

  • cd is the same as cd ~

  • reset

to reset your terminal 👍

mv moby-dick.txt{_,} will expand to: mv moby-dick.txt_ moby-dick.txt

You can always test what your command will do (without actually doing it), by adding echo - this will output just the expansion.

  • mkdir -p

The -p flag will create a directory if one doesn't exist yet.

Expansion permalink

  • cat b{ee,oo}p.txt

  • img{0...100}

Will generate 100 directories like img0, img1 etc.

  • >> file.txt

Append to file.

  • ls -l *.txt | wc -l

Will count the number of .txt files in the current directory.

  • sed -r 's/\s+/\n/g'

Converts all whitespace (tabs, spaces, newlines) into newlines.

  • cal

Displays the calendar.

  • cal November 2020

  • cal -3

Shows previous months, current month, next month.

  • date

  • date +F

Today's date in year-month-date format.

echo 'date' (with backticks) is the same as echo $(date)

If you run curl -s the -s flag will get rid of the progress bar. 👍

Bash Scripts permalink

  • #!/bin/bash

You need to put that on the first line of your bash scripts.

  • touch logger

You can add a .sh extension to your scripts, but not needed.

  • echo date + '%F %T' $1 >> date +%F.log

Append a message to a file with today's date (a logger of sorts). $1 is the variable.

  • chmod +x logger

You need to make the script executable. 👍

You can use $* (instead of $1) to grab the whole input - for multi-word inputs. If you are using $1, you'll have to add ''. 👍

  • while true; do node server.js; done

Restart the node server every time it crashes.

  • usr/local/bin

Where your scripts should live.

  • sudo cp logger /usr/local/bin/loggy

Copying our script to the scripts folder. From now on, we can execute it from anywhere by typing loggy.

Exit Codes permalink

  • echo $?

Read the exit code of the program you've just executed (0 means no error, non-zero fo unsuccessful).

  • test -f

Evaluates the expression and, if it evaluates to true, returns a zero (true) exit status; otherwise, it returns 1 (false). -f: True if file exists and is a regular file.

Jobs permalink

  • ctrl c

Stops the process.

  • ctrl z

Puts the process in the background. You can revive it with fg 👍.

  • pgrep

Search for a process by its name.

pkill -9 [process name]

Kill all processes which match process name.

Regex permalink

regex in JavaScript:

  • str.split(re)
  • str.match(re)
  • str.replace(re)
  • re.test(str)
  • re.exec(str)

flags:

  • i case insensitive
  • g match all occurrences (global)
  • m treat a string as multiple lines
  • s treat a string as a single line

metacharacters:

  • . matches any character

  • ? zero or one time

  • * zero or more times

  • + one or more times

  • [] character class

  • ^ anchor at the beginning

  • $ anchor to the end

  • (a|b) match a or b

  • () capture group

  • (?:) non capture group

  • \d digit [0-9]

  • \w word [A-Za-z0-9]

  • \s whitespace [\t\r\n\f]

Sed permalink

  • sed -E and grep -E

to escape metacharacters

  • sed -E 's/[0-9]+//'g

remove all the numbers

  • 'cool "beans" zzz'.replace(/"[^"]+"/, 'XXX')

Replace everything between double quotation marks with XXX.

  • sed -E 's/(\w+) \1/\1/g'

Remove duplicates (only if they are next to each other).

  • sed 's/cool/k00l/g' -i file.txt

Replace cool with k00l in the file specified.

Vim permalink

navigation (also used in less)

  • k upwards
  • j downwards
  • l right side
  • h left side