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

linux2

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

linuxfundamentalspart1 linux2

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

linuxfundamentalspart1 linux2

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.