Subshells
Sometimes the shell uses subshells to launch commands. They inherit the exported environment.
-
Every command launched by the shell is executed into this shell
-
You can force the execution of a command inside a subshell by:
-
putting parentheses
(...)around this command -
preceding it with the name of the shell (the very command that launches the shell, in our case,
bash)
-
$> time ( egrep "^[^,]{1,},{5}.*$" file.csv | cut -d',' -f 3,5-7 )
Subshells
-
The
bashconstruct$( … )launches a command in a subshell and treat the result as a variable content -
One can store the output of a command in a variable using this construct
$> val=$(wc -l large_file.txt)
$> echo $val
9385731
$>
Redirections
Commands usually:
-
Write their result on the standard output (the terminal)
-
Read their data from a file or from the standard output (the keyboard)
Redirections
However, it is possible to redirect standard input and output:
| > |
redirects the standard output of a command to a file, overwriting this file if it already exists |
| >> |
redirects the standard output of a command to a file, appending data at the end of this file if it already exists |
| < |
redirects the standard input of a command from a file |
$> ls *.txt > list_txt
$> ls *.adoc > list_adoc
$> cat list_txt list_adoc > list
$> ls *.html >> list
$> mail -s "Here is the file list" foo@bar.net < list
Pipelines
What if the output of a command could become the input of another?
This is what pipelines are all about, chaining commands to create a processing chain without using auxiliary files.
Pipelines
-
A pipe is created with the
|character ([AltGr+6] or [⌥+L]) -
Every command of a pipeline is executed into a subshell
-
It is possible to chain as many commands as necessary
$> ls | grep foo | sort -n -
The output of a pipeline can be redirected to a file
$> ps -efal | tr -s ' ' ',' | cut -d',' -f1,3- > processes.csv
Pipelines
-
The
teecommand duplicates a stream to a file$> ps -efal | tee ps.out | tr -s ' ' ',' | tee ps.csv | cut -d',' -f1,3- > processes.csv $> ls -tr ... ps.out ps.csv processes.csv
Executing into the background
Most commands prevent the use of the terminal they have been launched in until they are finished doing their job.
It may be useful to work around this behaviour to keep on working while a long process is going on in the background.
Executing a command into the background allows the user to get the prompt back before the command actually ends.
This way, several commands may be launched in parallel.
Executing into the background
It can be achieved in several ways:
-
Launch the command by adding the
&character at the end of the line$> long_process & [1] 12345 $> ... [1]+ Done long_process $>
Executing into the background
-
Pause an already launched command with the
[Ctrl+Z]shortcut, which brings back the command prompt -
Then use
fgorbgto resume it respectively into the foreground or into the background$> long_process ^Z [1]+ Stopped long_process $> bg [1]+ long_process & $>
References
-
"Unix Text Processing", Dale Dougherty and Tim O’Reilly, Hayden Books, 1987
https://www.oreilly.com/openbook/utp/ -
Christophe Blaess cheat sheets[FR]
https://www.blaess.fr/christophe/developpements/aides-memoires/-
Unix commands[FR]
https://www.blaess.fr/christophe/memo_commandes_unix.html -
Shell programming[FR]
https://www.blaess.fr/christophe/memo_programmation_shell.html
-
-
Rich’s sh (POSIX shell) tricks
https://www.etalabs.net/sh_tricks.html -
Bash Reference Manual
https://www.gnu.org/software/bash/manual/bashref.html
-
Advanced Bash-Scripting Guide
http://tldp.org/LDP/abs/html/ -
"Mastering Regular Expressions, 3rd Edition — Understand Your Data and Be More Productive", Jeffrey Friedl
https://www.oreilly.com/library/view/mastering-regular-expressions/0596528124/ -
"GAWK: Effective AWK Programming", Edition 4.1
http://www.gnu.org/software/gawk/manual -
Manual pages‡ :
bash(1),grep(1),regex(7),gawk(1)
‡ : read thoses pages on your own operating system, not on the Internet!