The CLI
Anatomy of a CLI Command
REPL
When looking at a CLI what you're looking at is often called a REPL (Read Evaluate Print Loop). It is an interactive way of programming where you're writing one line of code at a time.
For example you're CLI may start off in a bash REPL, then when you type python3
it will open up a python REPL.
Shells and Emulators
Terminal in mac for example is an emulator, within this emulator you can run many shells (via different tabs)
The emulator contains the shells that are running.
Bash is 30 a year old shell, stands for Born Again Shell.
NOTE: A shell is meant for running commands whereas a REPL is more for running instructions and function calls. Some examples of a shell are bash and zsh. Some examples of a REPL are python REPL and node REPL.
File System
A common command that shows where you are in the File System is pwd
.
NOTE: You are always somewhere within in the File System.
Help
Adding --help to any command pulls up documentation on how to use that command.
Navigating around
You can Change Directory with
cd
cd directory>
cd ..
cd ../directory>
NOTE: that the .. means go back a directory.
- You can chain moves together so long as the path exists.
- You can always check where you are with pwd.
Arguments / Parameters
You can see everything that is inside a directory with the list command.
ls
ls -al
Notice that the -al tag shows all the permissions and some extra information about everything in the directory.
You can also view what's in other directories that you are not in by supplying a path.
ls home/ubuntu
Another common command is echo. Which essentially just makes the console print out whatever is after echo.
It works similarly to console.log() in JavaScript.
To see the path of a program you can use the which command. For example, to see where the ls
command lives:
dempsey-:~$ which ls
/usr/bin/ls
NOTE: An argument is textual information you provide to a program
Flags
- Is additional text that tells the program to produce something else or something more.
- The standatd place to put flags is immediately after the program. This doesn't usually matter, but it can.
- Some Examples ls: -l - long form output -a - shows all hidden items in the directory as well -la -al - The previous to flags combined.
- You can see all the flags for a program through the --help flag.
- A single dash means that you are going to pass in multiple flags.
- To demonstrate this, take --help. This is the help command but if you were to write -help. This would be interpreted as -h -e -l -p.
- You can give flags a parameter like so:
ls --ignore=Desktop
ls --ignore Desktop
ls -I Desktop
ls --ignore=Desktop --ignore=Documents
ls -I Desktop -I Documents
The first 3 lines do the same thing, ignore the Desktop dir. The second last and last command ignore Desktop and Documents.
Tips & Tricks
Tilda
Go straight to the home directory with tilda (~):
cd ~
Slash
Go straight to the root directory of the project
cd /
Up and Down
Cycle through history of commands recently used with the UP
key.
As you'd expect, if you go past what you want you can backtrack with the DOWN
key.
Tab Completion
- Auto complete commands with
TAB
. It does this by knowing what is available and then if you start typing it auto-fills for you when you pressTAB
- You can
TAB
twice to see the options available to you for completing the command. - Very handy for working with paths as the computer knows what is available.
- If you are trying to
TAB
and what you expect isn't available, you probably aren't where you think you are.
Reverse Search
Ctrl
+ R
will start the reverse search program. A use case
for this is to find commands in your bash history. If you type ls into the program,
it will search for all ls commands that you have used in the past. You can keep searching
further back in the history by continuing to press Ctrl
+ R
. If you Press ENTER
it will run the current command. Pressing UP
or DOWN
will grab the previous or next command to the one that
was found and put it in your terminal, it won't automatically run it. ###
.bash_history A file that stores all the commands you have ran in the past. Can
be found in the following path ~/.bash_history
. Once you log out
of your terminal session, it writes the commands from that session to your bash
history. So it's not always writing to .bash_history ### !! Runs the previous
command. Often called "Bang Bang". This can be useful to pair with other
commands. See example below. bash chmod u+x file.txt sudo !!
This will
run the chmod again but as the super user. ### clear Clears the terminal.
Previous work can still be accessed by scrolling up. If you don't want to write
clear you can use Ctrl
+ L
Copy and paste on CLI To do so on terminal use Ctrl
+ `
Shift
+
Cand
Ctrl+
Shift+
V`
Just be very CAREFUL when doing so. If you're not on a trusted site, copy
and paste into a text editor and then view information to ensure it's what you
expect. This is because it isn't difficult to use javascript to change what's
actually copied on a website. For example a site can pick up that you're trying
to copy and it can put something else into your clipboard. TIP: Figure out
what you are actually copying and pasting! NOTE: Don't feel guilty about
copy and pasting. It is fine to copy and paste but learn what the command is
doing.
Signals and the Power of Ctrl
Commonly used for shortcuts or to send specific signals to bash.
Shortcuts
CTRL
+ A
- takes you to start of the line
CTRL
+ E
- takes you to the end of the line
CTRL
+ K
- "yanks" everything after the cursor
CTRL
+ U
- "yanks" everything before the cursor
CTRL
+ Y
- pastes back everything that you "yanked"
CTRL
+ L
- clears the screen
CTRL
+ R
- reverse search
Signals
Tells the program your intent but it is up to the program to do something about it.
CTRL
+C
: SIGINT, a signal interrupt that can stop programs mid execution, note this doesn't force the program to stop.CTRL
+D
: SIGQUIT, a signal that quits entire program and shutdowns the current session.
NOTE: Usually CTRL
+ D
is required if inside a REPL in your shell. Like python for example.
SIGTERM: If you use the kill program to kill another program, it does this by sending a SIGTERM to the program.
SIGKILL: If you want the program to STOP NOW you can kill -9 or kill -SIGKILL and it sends a SIGKILL where the program doesn't worry about clean up, it just stops now.
To lists all the available kill signals:
kill -l