Home > other >  Bash execution process
Bash execution process

Time:12-03

I wrote a bash script like this:

#!/bin/bash
var=2
echo result: $(expr "$var"   1) > test.out

I run it from the shell with ./test command and it creates the file test.out and the output into the file

result: 3

I'm curious to understand more in details the underling steps the bash uses to substitute the values, redirect the stdout and produce an output into the file.

Could you please explain how the OS is handling the execution of this script?

CodePudding user response:

Here a simple schema with the main steps that happens after the execution of the script:

* --> Script execution -> Shell --> Expansion --> Command execution

Before the execution of the command inside the script, first the shell prepares the commands as filters with redirection and input/output pipes. After that, there is the shell expansions when the shell looks for special characters and substitues them if any.

About the substitution, it follows this order:

  1. substitution of the commands: the command among backquotes are executes and substituted by their result
  2. substitution of variables and parameters: the values of the variables (ex. $var) are expanded
  3. substitution of metacharacters (* ?) into filenames with pattern matching
  • Related