Bash Tips and Tricks

(Quality coding #1: Getting situated)

Dan Fitch



What are we doing?

Well, it depends on what people want!


Bringing up and answering questions on the fly


No digression is too strange, nothing is too simple to ask a question about

What about different skill levels?

Some sessions will be targeted at different skill levels.

AKA, making none of the people happy some of the time

Constant improvement

Please email me comments to help improve this. I'll put up an anonymous survey by the next time we gather.

Three things to cover today:

  • fasd
  • code review
  • intro to bash



Think of it like a history that reads your mind?

"Fasd offers quick access to files and directories for POSIX shells. Fasd keeps track of files and directories you have accessed, so that you can quickly reference them in the command line."

"The name fasd comes from the default suggested aliases f(files), a(files/directories), s(show/search/select), d(directories)."


is really the only way to see the power

"z" to jump dirs
"zz" to jump dirs interactively

"o" to open:

  • linux: alias o='a -e xdg-open'
  • mac: alias o='a -e open'
  • windows: alias o='a -e start'

editor: alias e='f -e vim'

(and it can also integrate with spotlight's "recently opened" list or other editors)

use in complex commands, like cat `f something`

or cp `f results csv` `d psy tmp`

careful... why?


Code review

What are my goals?

Good code review

  • Catch bugs, spot edge cases
  • Learn what's confusing
  • Make it maintainable
  • Think about testing
  • Get comfortable with other people's code

Bad code review

  • Forgets that we all screw up
  • Insults the person or code ("wrong", "bad")
  • Makes it harder to read

Let's look at some bash scripts!


Whirlwind Intro to Bash

Goal: You'll jump in the pool, and be able to search your way out of trouble!

Experts can now run away...

Show of hands

Have you... Never used a command line shell?
Have you... used a command line a little?
Have you... edited your .bashrc?
Have you... made your own alias?
Have you... written a script?

Apology time

I wanted to make this interactive, but I'm not sure how much time we're going to have at this point.

If you did learn how to SSH in, great! SSH to a login server. Let's try a few things as we go.

Looking around


List stuff in cwd

cwd? pwd?

ls dirname

List stuff in a directory

ls -alrt

any guesses?

List All (Long) details, Reverse Time order


cd dirname

Go into a directory

what does cd stand for?

cd ..

Go up to the parent directory

what is . then?

mkdir name

Make a new directory

Looking in files


Show a whole file


Scroll through a file


Show top/bottom lines



store value in $a

y $a

run y, passing value in a

export a

put a in shell context for child processes


x | y

pipe output of x to program y

y `x`

like getting the result of running x and then pasting it as parameters to y

x > file

pipe output of x to file, starting fresh

x >> file

pipe output of x to file, appending

I was going to put more realistic pipeline examples but I ran out of time



stop job


background job


foreground job

command &

start job in background

Processes, continued


List all processes


List all processes, but constantly updating


Kill off a process (or send a signal)

.bashrc and aliases


I'll show mine but it's a mess

alias x='long command'

shorten your typing!

any ideas for shortening?

alias ..='cd ..'



list commands


run the numbered command


re-run the last command

!! param

any guesses what this does?

re-run the last command changing the last parameter to param

Handy stuff


run an arbitrary command every N seconds

tail -f

watch a file as it is appended


keep a session going


(or, Q&A)

thanks for your patience as I figure out how to host these