Command Basics
Commands usually have an option "help". βͺ
$ man -h
$ find -help
$ gcc --help
β οΈ There is no echo/feedback when writing a password.
Strings manipulation
When using commands, you will most likely use, or have to use strings. Most of the time, quotes are optional, and command arguments do not need to be quoted π―.
$ echo Hello, World
$ echo 'Hello, World'
$ echo "Hello, World"
Concatenation is also implicit. All of these are the same:
$ cat abc
$ cat "a"bc
$ cat "ab"c
$ cat "abc"
$ cat "a""bc"
There are times when we must quote an argument π£. For instance, when there is a space inside a name/path.
$ ls /Documents/my bookmarks/ # β
$ ls "/Documents/my bookmarks/" # β
$ ls '/Documents/my bookmarks/' # β
$ ls /Documents/my\ bookmarks/ # β
π The main difference between single and double quote is that in the former, variable are NOT interpreted. Refer to the scripts section.
To calculate the length of a string:
$ echo -n "xxx" | wc -c # use wc
$ v="xxx"; echo ${#v} # use variable length
expr
: substring, indexOf...
Length of a string (π)
$ expr length "Sarah"
5
Extract a substring ("3" characters in "Sarah" from index "2")
$ expr substr "Sarah" 2 3
ara
Index of a character in a string (index of S/w in Sarah)
$ expr index "Sarah" S
1
$ expr index "Sarah" w
0
Calculations
You may want to do some stuff involving calculations. The shell won't interpret any calculations, so you must use $[...]
or $((...))
to evaluate something.
$ four=$[3+1]
$ four=$((3+1)) # same
Example of increasing a variable by one (loops are covered in scripts).
i=0
while [ $i -lt 5 ]; do
i=$[i+1]
done
Pipes
It's possible for a command output, to be used as the next command input using a pipe: their_output | is_my_input.
Example π₯:
The command wc -l
is used to count the number of lines in a text. The text could be a file, or some input like below:
$ wc -l
aaa
bbb
2 # two lines
β‘οΈ As a reminder, use CTRL+D
to indicate the end of the input.
The command find . -name toto
find recursively every file named "toto" starting from the folder '.
'. Its output is something like:
$ find . -name toto
./mem/all_toto_copy/toto
./mem/all_toto/toto
[...]
./mem/toto
Using a pipe, we can send the output of find
to wc
, and find the number of files (lines) returned by find
:
$ find . -name toto | wc -l
57 # find returned 57 lines
Chaining commands
There are three reasons to chain commands:
- β
execute a command if the previous one was successful (
&&
) - β execute any command until one is successful (
||
) - π€ put many commands in one line/statement (
;
)
For the two first ones, a command is said to be successful is the process exit code is zero. Otherwise, it failed.
$ cat not_found # a non-successful command
$ echo $?
1 # failure
Examples π₯:
Execute two commands in one go
$ ls toto; ls tata
Execute the second ls
if the first was successful.
$ ls toto && ls tata
Execute the second ls
if the first failed.
$ ls toto || ls tata
Redirections
Redirections allow us to:
- π fill
stdin
with a source such as a file - ποΈ redirect
stdout
to a file - π© redirect
stderr
to a file
There are multiple and varied uses:
- π fill commands needing user input with data from a file
- π§βπ store results that we took a long time to compute, to avoid losing them or having to compute them again
- π§― redirect error to
/dev/null
(discard errors) - πͺ² store errors for debugging purposes
- ...
Examples π₯:
Redirect stdout
to a file (save result)
$ echo "Hello, World" > myFile
Append stdout
to a file
$ echo "Hello, World" >> myFile
Use a file instead of user input
$ tee < original.txt
Hello, World
Redirect errors to a file/discard them
$ cat not_found 2> onl_errors_will_be_stored_here
$ cat not_found 2> /dev/null # discard them
Redirect stdout
tostderr
(uncommon usage)
$ >&2 echo "xxx"
$ echo "xxx" >&2
Merge stderr
with stdout
(uncommon usage)
$ cat not_found 2>&1
Discard stdout
, but keep stderr
(uncommon usage)
$ echo xxx &> /dev/null
Redirect both stdout
andstderr
(uncommon usage)
$ echo xxx > output.txt 2>&1 # β οΈ Order is important
Special
Command substitution
Command substitution is mainly used in scripts, to store the result of a command inside a variable. In a nutshell, a command output is used as a parameter of another command: xxx $(yyy) or xxx `yyy`.
Example π₯: all of these commands are the same
$ ls ~
$ ls `echo ~`
$ ls $(echo ~)
Subshell
A subshell is a shell that is launched within another shell, allowing us to execute commands without changing the parent shell environment.
Example π₯: In the example below, we create a variable in a subshell, and print it. We also try to print the variable outside the subshell, but since the variable does not exist, the output is empty.
$ (cat=toto; echo "1. $cat") && echo "2. $cat"
1. toto
2.
π It could be used to temporarily change location, set an environment variable for one command...
π» To-do π»
Stuff that I found, but never read/used yet.
- terminal shortcuts (CTRL +/-/L/D, scroll button...)
- CTRL+SHIFT+T to open a terminal.