Beginner Lab 2 - Core Shell
Facilitator: Ethan Zhang8 min read
- Setting up
- SSH (Secure Shell)
- Pipes and Redirection
- A quick intro to
- A quick intro to tmux
- Submitting the lab
Welcome to Lab 2! In this lab you will be learning how to work productively in a shell.
Remember to submit your answers in the Gradescope assignment!
Don’t forget to use Google and
man when stuck. The resources linked at the bottom may be helpful as well.
This lab requires a bash shell,
If you do not have tmux:
SSH allows you to log in to a remote computer through the internet. It is the equivalent of opening a shell on a remote computer.
The usage is
ssh [remote username]@[remote host].
- Log on to
tsunami.ocf.berkeley.eduwith your OCF username and password. There is a file in
~staff/public_html/decal. Open it. What is the secret in the file?
Chaining together commands is essential to automating your way through the shell. Here’s a quick cheat sheet:
> : Redirect stdout to a file (Will overwrite the file).
>> : Append stdout to a file (same as
> except does not overwrite).
< : Read input from a file.
| : Send output from one program to the input of the next.
Here’s an example: let’s say you’re taking a class that needs you to submit a text file with your SID in it. Your first instinct might be to open up a text editor (like
vim) and simply type it in, but there’s a faster way to create files! Here it is:
echo '123456789' > sid.txt
Scrabble is a fun word game that rewards players for knowing a bunch of weird words. I’d like to add some more words to my aresnal!
For the next couple questions, download this text file containing all valid Scrabble words using
curl, or a similar command. We’ll be having some fun with it!
- I get pretty stuck when I get a rack of all consonants! Help me find some words that don’t contain any vowels by writing a command to print out the first 10 words that do not contain any of the letters A, E, I, O, U, or Y. Text manipulation commands like
sedmight be useful (you probably won’t need all of them). This regex testing site may also come in handy.
BRR BRRR CRWTH ... (7 more lines not shown)
- In Scrabble, you can get a Bingo (and +50 points!) if you use all 7 of your letters in the same turn! I’ve got the letters ‘CAL’ in my rack and want to score those big style points with them. Write a command to fetch all of the words containing ‘CAL’ that are 7 or more letters long, and save them to a file called
ABAPICAL ACALEPH ACALEPHE ... (380 more lines not shown)
!! can be used to repeat the previous command in the shell.
python program.py sudo !! == sudo python program.py
!:[num] is treated as the previous command’s [num] argument.
touch doc.txt vim !:1 == vim doc.txt
Note: This behavior is from bash and some other shells that implement it.
vim is a very widely used text editor. It’s well known for its customizability and plethora of keybinds. While it may be somewhat unintuitive to use at first (since a lot of common keybinds for things like copy-paste, saving, or exiting don’t do what you think they will), it’s well worth learning about, and you’ll certainly come across it all the time when working in the shell!
- It’s a descendant of vi, which was written in Berkeley by Bill Joy, who went on to found Sun Microsystems.
- Sometimes you will be suddenly thrown into
vimvia merging git conflicts or other programs.
- It’s included in practically every UNIX environment.
- You can be very productive when familiar with it.
To get started with learning
vim, run the command
vimtutor. This will walk you through the below material in an interactive manner! You aren’t required to finish the entire tutorial, but we encourage you to at least complete lesson 1. You can then use this section as reference if you forget anything.
Vim is a modal text editor, meaning that you can change editing modes in order to do different things. There are 3 primarily used modes: Normal, Insert, and Visual mode.
- Used for moving around and performing actions
hjklto move left, up, down, and right (arrow keys work too but hjkl is usually faster to use!)
Gto move to end of file,
ggto move to beginning
ito enter insert mode (
oalso change mode in different ways)
ddto cut a line
yyto copy a line
- Type in commands with
- Save with
- Exit with
- Save with
- Explore more commands online! Here’s a cool cheat sheet to get you started.
- Used for editing text like a usual editor
- Arrow keys to move
- Esc to exit to normal mode (lots of people bind it to Caps Lock)
- Enter with
vfrom normal mode
- Used to select text visually
- Modify selection with normal mode movement commands
oto move the cursor to the other side of the selection
- Yanking, deleting, and pasting use
A key feature of
vim is chaining together commands.
Normal mode is essentially a massive amount of shortcuts that you can combine to quickly navigate and edit a file.
Want to move down 3 lines?
You know that
j means move down 1 line, so you can use
3j to move down 3.
d is for deletion and
w is to jump to the next word, so what does
Try playing around with lab2.md while looking up some new commands. Use
wget to download it!
- How would you delete the previous 10 lines?
- How would you jump back to the shell without exiting
- How would you edit a new file alongside another file?
- How would you indent a block of text?
- Tell us about one other cool vim feature you found out about that isn’t mentioned in this lab!
- Use this cheatsheet
- You can open multiple windows when sshed into a machine.
- You can go compile and run programs while editing them.
- You can logout and ssh back in without having to reopen all your files.
- Start a session with
- Detach from a session with
Ctrl-b d(press d after releasing
- Split into 2 panes with
Ctrl-b %(vertical) or
- Swap current pane with
- Find more information about tmux online. You might find this cheat sheet helpful!
- Make a new tmux session. Using tmux shortcuts, try to make your session have a similar layout to the one below, and upload a screenshot of it to Gradescope!
Some things to note:
- The top left panel is resized. By how much, it doesn’t matter.
- The top right panel is named “Hello World”. (You can see this name displayed on the bottom left.)
- You don’t need to run any of the commands I did, but they do look pretty cool :) Try to figure out what command the bottom panel is running, and what it does!
- Don’t worry about copying the layout exactly. The purpose of this exercise is simply to help you get comfortable making custom layouts in tmux.
- If you haven’t already, detach from your current tmux session using
Ctrl+b d. Now, what command would you type to attach back to it?
- What command will delete your session?
- What command will create a new session?
- Tmux can be used to share your session with others.
- Try remapping shortcuts like the prefix
Ctrl-bto something more convenient.
Ctrl-b [can be used to scroll around buffers and copy things.
Once you’re done remember to submit your answers and screenshot to Gradescope. There are multiple valid answers for some of the questions. Don’t be stressed about getting something correct; just have fun exploring. We’ll release the answers after the lab is due!