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 bash construct $( …​ ) 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
meaningful for commands that only read their data from the standard input

$> 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 tee command 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:

You may:
  • 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

Or
  • Pause an already launched command with the [Ctrl+Z] shortcut, which brings back the command prompt

  • Then use fg or bg to resume it respectively into the foreground or into the background

    $> long_process
    ^Z
    [1]+ Stopped  long_process
    $> bg
    [1]+ long_process &
    $>

References

‡ : read thoses pages on your own operating system, not on the Internet!