A Command-line interface (CLI) is a means of interacting with a computer program by inputting lines of text called command-lines. Command-line interfaces emerged in the mid-1960s, on computer terminals, as a user-friendly alternative to punched cards.
In computing, a shell is a computer program that exposes an operating system's services to a human user or other programs. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer's role and particular operation. It is named a shell because it is the outermost layer around the operating system.
Each time we launch the terminal application, it creates a new session. The session immediately loads settings and preferences that make up the command line environment.
We can configure the environment to support the commands and programs we create. This enables us to customize greetings and command aliases, and create variables to share across commands and programs.
The environment refers to the preferences and settings of the current user.
shows the path to that command - where the actual binary is located as determined by $PATH environment variable
which python3
which ls
General syntax of commands
Broadly, there is a command and then maybe various arguments. The arguments can then be further broken down into options/switches/flags, parameters, and targets.
| - | |
<> are often used in Syntax guides to indicate that the <> surround a place for a command rather than an actual command. | is used to denote OR or Optional so in the line above we read that a Command can be used on its own but may be followed by Switches (single letters, preceded by a hyphen that adjust what the command does), Parameters (Things that the command needs to know in order to work) and a Target (The thing that the command will be applied to).
switches aka options aka flags
often work either shorthand with one dash -U or with a full name -user
Evaluate to Boolean
Shorthand -a
Longhand --all
e.g.
ls -l /home/brian
- means List in Long Format (the -l switch) the directory (target) /home/brian
Exit
exits terminal
Keyboard commands
Tab
Autocomplete partially typed commands, files, etc.
If there are multiple possibilities, tab won’t complete, but if you double tap tab you’ll get a list of the possibilities
Good for finding the names of options and switches
Good for finding all the apps that start with a set of letters with apt-get install for example
Up arrow
Cycles through previous inputs
Ctrl - C
Halts the current command, cancel the current operation and/or start with a fresh new line - sends a sigint
q
quits a terminal mode, like in less, e.g. - Ctrl-c doesn’t work in less. I think it might be in vi or something
Ctrl - Z
Stops the current command, resume with fg in the foreground or bg in the background
Ctrl - R
Recent commands
Can search them as well?
Ctrl - L
Clears the screen leaving your typing there
Equivalent to the clear BASH command
Ctrl-D
Exits the shell
Ctr-alt-del
REISUB
Ctrl-A
Cursor to start of line
Ctrl-E
Cursor to end of line
Ctrl-U
Cut/Delete left of cursor
Ctrl-K
Cut/Delete right of cursor
Ctrl-W
Cut/Delete word to left of cursor
Ctrl-Y
Paste what you cut via Ctrl-U or K or W
$
Dollar prompt
#
Shown at prompt when root
!!
Repeats last command
E.g. If you forget sudo you could just type "sudo !!"
!$
Repeats just the last argument
E.g.
Mkdir ~/docs/Evan
Cd !$
&
after last command to tell terminal to run it in the background, thus freeing up the currnet terminal for further use
e.g.
sudo apt-get upgrade &
ping google.com & ping apple.com &
runs both commands in background
It will output a process id number which you can check later with ps or kill with kill
You can keep doing this indeifintely with multiple commands to be sent as a background process
History
Up Arrow
cycles through previous terminal commands
history
command to list your terminal history
!num
!53
executes specific numbered command
!num:p
To edit a command from history
This prints the comnand instead of executing it so that you can then arrow up to edit it
history with datestamps
$ HISTTIMEFORMAT="%F %T " $ history
This is only valid for the current session, but you can also add this to your bash profile if you want that to be permanent
sudo !! trick to repeat last command with sudo prepended
omnioutliner:///open?row=dOpCUN901UY
Recursion
-r or -R
changes attributes of all files inside a folder instead of just the folder itself
-y
automatically answers yes to following confirmations
Clear
Clears everything on the screen
equivalent to the ctrl-L keyboard shortcut
Pause output per screen
less
usually per line?
a pun on more
q to quit
I think it runs in vi
more
usually per page?
you have to pipe the commands output to less or more
Variables in functions are global by default unless declared local with local variablename=value
A variable in bash can contain a number, a character, a string of characters. You have no need to declare a variable, just assigning a value to its reference will create it.
Variables need to be preceded by $ when referencing them, but not when assigning them.
Variable names are case-sensitive
Variables are global by default, even in functions. local var=value declared it as local
Example
STR="Hello World!" echo $STR
Delete Variable
unset variable_name
$1 takes the value of the first argument after the script automatically
"The xargs command in UNIX is a command line utility for building an execution pipeline from standard input. Whilst tools like grep can accept standard input as a parameter, many other tools cannot. Using xargs allows tools like echo and rm and mkdir to accept standard input as arguments."
execute arguments
echo 'one two three' | xargs mkdir ls one two three
"By default xargs reads items from standard input as separated by blanks and executes a command once for each argument. In the following example standard input is piped to xargs and the mkdir command is run for each argument, creating three folders."
Redirection
Instead of output going to screen it goes to a file
e.g.
ls -l Documents > documents.txt
sort < bands.txt
sort < bands.txt > bands_sorted.txt
makes the output appended to the file instead of overwriting
<
goes the other way
redirects stdin instead of stdout?
e.g.
cat gymnastics.txt is the same as cat < gymnastics.txt
cat volcanoes.txt | wc | cat > islands.txt
Multiple commands
Order matters
commands are interpreted in order
Multiple Commands with Operators
- &&
- run second command only if first command completes successfully
- Ie, && is boolean so it requires both to be ‘true’
- e.g.
sudo apt-get update -y && sudo apt-get upgrade -y || echo “Something
went wrong”
- ||
- run second command only if first command doesn’t complete successfully
- Ie, boolean logic
- you can try using || to echo something to report if the previous sequence ran successfully - like { command && command } || echo something went wrong
- ;
- run second command regardless of if first command completes successfully
- ie, just a simple sequence, in order
- {} Brace Expansion #remember
- to group commands together like parentheses in logic
- e.g.
- command && echo command ran correctly || { echo command did not run correctly; commandtodointhatcase; }
- need a space between the curly brackets
- need ; at end of last command to return to shell correctly
- Better example:
- cp {main.js,manifest.json} $obsidianVault/.obsidian/plugins/obsidian-wikipedia-data/"
- instead of having 2 separate cp commands, this will effectively do 2 cp commands - 1 for each item in the braces.
Multiple Commands with Conditionals
if
then
else
fi
e.g.
if ls *.txt ; then echo We’ve found text files ; else echo No files found ; fi