Introduction to Bash
Matt Thomas - Lead Instructor (Full Stack/DAP)
https://github.com/matsinet
https://where.matsinet.codes
It's only growing with electric (Telsa) & self-driving cars, land & air traffic control systems, automation, space travel (ISS & SpaceX).
or as you probably know it better as...
This is the oldest shell and as such is not as feature rich as many of the other shells.
The Bash shell is a combination of features from the Bourne Shell and the C Shell. It's name comes from the Bourne Again SHell. It has a command-line editor that allows the use of the cursor keys in a more "user friendly" manner than the Korn shell. It also has a useful help facility allowing you to get a list of commands by typing the first few letters followed by the "TAB" key. It is the default shell on most Linux distributions.
ZSH, also called the Z shell, is an extended version of the Bourne Shell (sh), with plenty of new features, and support for plugins and themes. Since it’s based on the same shell as Bash, ZSH has many of the same features, and switching over is a breeze.
Outputs manual aka "man" page that explains how to use the specified command
man <command>
Uses VI navigation and exit commands (To be explained later)
bash and z shell both support tab completion at the command line
{partial command, file name, directory name}[tab]
Type a string that makes up the beginning of a command, file name and directory name and press the tab key to complete the exact match.
{partial command, file name, directory name}[tab][tab]
If there is no exact match, no results will show, but doing it twice [tab][tab] to get a list of possible matches.
List file & directories (DOS equivalent is dir)
* is the wildcard
ls -l
List files and directory long format and show permissions
ls -a
List files and directory including hidden files
ls -h
List files and directory using human readable file size
ls -lah
Combining them all together
Create a new directory
mkdir {directory name}
Alias | Description | Purpose |
---|---|---|
. | period/dot | Current directory |
.. | double period/dot dot | Parent directory |
~ | tilde | Home directory (/home/{username}) |
- | dash/minus | Previously navigated directory |
/ | slash/front slash/whack | Directory separator, Windows uses \ (back slash) |
/ | slash/front slash/whack | Root directory, when used at the beginning of a path |
Change directory
cd {path to any directory}
cd ..
(double period) Change to the parent directory
cd ~
(tilde) Change to your home directory
cd -
(dash) Change to previous directory
pwd
Output your current location
Create a new file
touch {file name}
Copy
cp {source} {destination}
Copy a file, destination default is the current directory & can be omitted.
cp -R {source} {destination}
Copy recursively including directories
Destination default is the current directory (.) & can be omitted.
Remove
rm -R
Remove recursively
rm -f
Remove forcefully i.e.: don't ask if this is what I really mean!
Linux Shell does NOT have a recycle bin! ... Be warned, there is no turning back.
rm -Rf
Remove recursively and forcefully.... Again be warned!
Command History, lists commands executed recently
Use the !## (Bang aka Exclamation Point) to execute a previous command
Up and down arrow keys can be used to navigate previous commands as well
Search command history, right on the command line
Press ctrl + r and type a search string, press enter key to accept
Example: `npm`
Outputs the contents of a file to the terminal
cat <file(s)>
dot files
Dotfiles are used to customize your system. The “dotfiles” name is derived from the configuration files in Unix-like systems that start with a dot (e.g. .bash_profile and .gitconfig)
Nano
Vim (VI)
GNU nano is a text editor for Unix-like computing systems or operating environments using a command line interface. It emulates the Pico text editor, part of the Pine email client, and also provides additional functionality.
There is only 1 mode in Nano, Edit.
Commands are executed using Ctrl (^) + Letter combinations i.e.: ^x to exit.
Linux: http://www.vim.org/
Mac: http://macvim-dev.github.io/macvim/
Vim is found on EVERY linux computer and is available via the command line.
Vim includes most all basic features from VI. For this Intro they can be interchanged.
Vim also has numerous additional features inlcuding:
This will open the file in command mode. What is command mode?
Command (esc)
Enter commands such as goto line, search, find and replace
Insert (i,I,a,A,o,O)
Insert new characters, Append new characters, Open a new line and then insert characters
Replace (R)
Enter insert mode while replacing characters instead of "pushing them"
Command | Description |
---|---|
c | Change |
y | Yank Character (Copy) |
yy | Yank Line (Copy) |
x | Delete Charactor (Cut) |
d | Delete, requires movement, quantity optional |
dd | Delete Line (Cut) |
p | Put (Paste) |
. | Repeat Last Command (Period) |
u | Undo |
Ctrl-R | Redo |
<quantity>command<movement>
All commands are cASe SeNsiTive
Command | Description |
---|---|
2dw | Delete the next 2 words |
2cw | Delete the next 2 words and drop into insert mode |
By default Vim uses only the basic keyboard so moving the cursor is down with...
h j k l
Arrow Keys also work most of the time, which I prefer.
Movement | Description |
---|---|
b | Beginning of word |
w | End of word |
0 | Beginning of line |
$ | End of line |
gg | First line of the file |
G | Last line of the file |
There are 3 types of visual mode, character, line & block
Mode | Description |
---|---|
v | Characters - Select consecutive characters |
V | Line - Select consecutive lines |
Ctrl-V | Block - Select continue block in either horizontal and/or vertical directions |
We are just scratching the surface of visual mode, more details here: http://vimdoc.sourceforge.net/htmldoc/visual.html
"n" & "N" will take you to next (n) and previous (N) search string.
aka: substitute
Search and replace with the "g" option will not ask for confirmation.
When you want to force an action regardless of the consequences use an exclamation point after the command.
A mentor told me to think of the exclaimation point as
Thanks Gregg
Example: To overwrite a file when it should throw a read-only error
use :w!
(write damn it!)
:q!
Be careful as explained in the previous slide the ! has consequences, in this case all modifications to the file will be lost.