Terminals
Terminals
returns their output. The program that actually runs that command is called a shell
Shells are often called "REPLs", standing for:
Read, Eval(uate), Print, Loop
i.e., shells read the commands you type, evaluate those commands (usually by
running other programs), print the output of those commands, and give you a new
prompt to type and repeat
the cat command prints the contents of a text files. Multiple files can be listed,
which will print in succession.
The head command prints the first n lines of a file, where n is a specified number:
ex) head -n 10 file1.txt
tail is similar, but it prints the last n lines.
The more and less commands prints the contents of a file one page/line at a time.
less is functionally the same as more, but has more features. You generally should
use less, but use more if less isn't installed.
You can use the less command with the -N flag to show line numbers (i.e. less -N
filename.txt)
use spacebar to scroll down a page, and b to scroll up a page.
The move command (mv) can move a file or directory to a new location or reame a
file.
Permissions:
First characher tells you if you're looking at a file or directory:
-: regular file (-rwxrwxrwx)
d: directory (drwxrwxrwx)
The next 9 characters describe the permissions of Owner, Group, and Others in
that order. Each group of 3 character represent reading, writing and exexutiing in
that order.
rwx: all permissions
rw-: read and write, but not execute
r-x: read and execute, but not write
sudo elevates a command to the root user (superuser), which has access
to everything and can do anything.
be cautious with what commands you use as superuser:
rm (remove) with the flags r and f run on the root directory will
delete all files on your system.
r (recursive) deletes everything inside and f (force).
chown (change owner) changes the owner of a file.
Compilation vs Interpretation:
A compiled program is a program that has been converted from a human-readable
source code into machine code (binary.
Programming languages like Go, C, and Rust are compiled languages that
produced compiled programs.
Interpreted Programs are programs executed by another program. The program which
executes an interpreted program is called an interpreter.
Interpreted languages include Python, Ruby and JavaScript and .sh shell
script files.
You can run any executable by typing its file path into your shell.
To run an executable that requires an interpreter, the computer needs to be told
what program to use.
This is accomplished by a "shebang", a line at the top of a script. It's
format is:
#! interpreter [optional-arg]
sh - The Bourne shell. This is the original Unix shell and is POSIX-compliant.
It's very basic and doesn't have many quality-of-life features.
bash - The Bourne Again shell. This is the most popular shell on Linux. It
builds on sh, but also has a lot of extra features.
zsh - The Z shell. This is the most popular shell on macOS. Like bash, it does
what sh can do, but also has a lot of extra features.
Both zsh and bash are "sh-compatible" shells, meaning they can run .sh scripts, but
they also have extra features that generally make them more pleasant to use.
For your purposes, the differences between zsh and bash are not super significant.
Echoing $PATH lists all directories the shell will look when executing
commands, each seperated by colons (:) ex.
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
use the nano command to edit files: nano ~/.bashrc
use the man (manual) command to bring up docummentation for programs. ex) man ls
By convention, most production-ready CLI tools have a "help" option that will print
out some information about how to use the tool. It's usually accessed with one of
the following:
--help (flag)
-h (flag)
help (first positional argument)
To find an exit code for a program that ran unsuccesfully, use echo $? after
receiving the error.
0 means no error
1 is used for a default or catch-all error.
Others are more specific.
Standard Output (Standard out, stdout) refers to the default place where programs
print their output. For Python that's print(), and shell that's echo.
Standard Input:
default way a programs reads input text. In python thie is input().
ex) name = input("What is your name? ")
print("Hello,", name)
Piping (|): takes stdout of the program on the left and "pipes" it into the stdin
of the program on the right:
ex) echo "Have you heard the tragedy of Darth Plagueis the Wise?" | wc -w
# 10
Sometimes a program will get stuck and you'll want to stop it. Common reasons for
this are:
You made a typo in the command and it's not doing what you want
It's trying to access the internet but you're not connected
It's processing too much data and you don't want to wait for it to
finish
There is a bug in the program causing it to hang
In these cases, you can stop the program by pressing ctrl + c. This sends a
"SIGINT" signal to the program, which tells it to stop.
Kill
kill PID
PID stands for "process ID". Every process that's running on your machine has
a unique ID. The ps, "process status" command can be used to list the processes
running on your machine, and their IDs:
ps aux
The "aux" options just mean "show all processes, including those owned by
other users, and show extra information about each process".
Unix Philosophy:
1. Write programs that do one thing and do it well.
Why programs like ls, grep, and less exist. They do one thing and
do it well.
ls lists files and directories; grep searches for text in a file;
less displays text
Neovim:
You start in "normal" mode. Press "i" to enter insert mode to edit text. Hit
esc to re-enter normal mode.
type :w to save and :q to exit.