Bash Tips and Tricks

(Quality coding #1: Getting situated)

Dan Fitch

2020-09-08

Goals

What are we doing?

Well, it depends on what people want!

Interactive

Bringing up and answering questions on the fly

Understanding

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

fasd

Huh?

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)."

DEMO

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?

Discussion

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!

Discussion

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

ls

List stuff in cwd

cwd? pwd?

ls dirname

List stuff in a directory

ls -alrt

any guesses?

List All (Long) details, Reverse Time order

Directories

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

cat

Show a whole file

less

Scroll through a file

head/tail

Show top/bottom lines

Variables

a=value

store value in $a

y $a

run y, passing value in a

export a

put a in shell context for child processes

Pipelines

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

Processes

Ctrl-Z

stop job

bg

background job

fg

foreground job

command &

start job in background

Processes, continued

ps

List all processes

top

List all processes, but constantly updating

kill

Kill off a process (or send a signal)

.bashrc and aliases

~/.bashrc

I'll show mine but it's a mess

alias x='long command'

shorten your typing!

any ideas for shortening?

alias ..='cd ..'

History

history

list commands

!2

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

watch

run an arbitrary command every N seconds

tail -f

watch a file as it is appended

tmux

keep a session going

THE END

(or, Q&A)

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