Skip to content

Commit

Permalink
Merge pull request #4 from Pro-tutorials/03-how-bash-process-commands
Browse files Browse the repository at this point in the history
add how bash process commands
  • Loading branch information
sammou00 authored Mar 6, 2024
2 parents 4b18161 + f0af189 commit a86bbfc
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
51 changes: 47 additions & 4 deletions 03-how-bash-process-commands/01.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
#!/bin/bash

# Author: Samir M.
# Date: 2024-03-04
# Modified: 2024-03-04
# Description: Brace expansion
# Usage: ./01.sh
# Date: 2024-03-05
# Modified: 2024-03-05
# Description: How bash processes command lines
# Usage: ./01.sh


# Bash goes through 5 steps to interpret a command line.

# Step 1: Tokenisation:
# The command line is broken into tokens. Tokens are the smallest units of a command line.
# Tokens are separated by metacharacters, such as spaces, tabs, and newlines, | ; & () < >.

# example
# echo "Hello, World!" # 4 tokens: echo, "Hello, World!", newline

# What is the differnce between a word and an operator?
# A word never contain unquoted metacharacters.
# An operator always contains unquoted metacharacters.

# example
# echo "Hello, World!" # 3 words: echo, "Hello, World!"
# echo "Hello, World!" # 1 operator: newline

# Step 2: Command identification:
# Bash identifies simple or compound commands.

# example
# echo "Hello, World!" # simple command
# if [ -f file ]; then echo "File exists"; fi # compound command

# Step 3: Expansions:
# Bash do expansions on the tokens. Expansions are the process of replacing a token with its value.

# example
# echo * # expansion of * is the list of files in the current directory

# Step 4: Quote removal:
# Bash removes quotes from the tokens.

# example
# echo "Hello, World!" # "Hello, World!" is removed

# step 5: Redirection:
# Bash performs redirections and then executes the command.


exit 0
32 changes: 32 additions & 0 deletions 03-how-bash-process-commands/02.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# Author: Samir M.
# Date: 2024-03-05
# Modified: 2024-03-05
# Description: Quoting
# Usage: .\02.sh

# Quoting: is about removing the special meaning of characters.
# There are three types of quotes in bash: single quotes, double quotes, and back slashes.

# Single quotes:
# Single quotes are used to prevent the interpretation of all of the special characters.

# example
filepath1='\home\sam\My Documents'
echo $filepath1 # \home\sam\My Documents

# Double quotes:
# Double quotes are used to prevent the interpretation of most of the special characters, except $ , back ticks ` and \.

# example
filepath2="\home\\${USER}\My Documents"
echo $filepath2 # \home\sam\My Documents

# Back slashes:
# Back slashes are used to prevent the interpretation of the next character.

# example
echo Hello John \& Jane # Hello John & Jane

exit 0
40 changes: 40 additions & 0 deletions 03-how-bash-process-commands/03.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

# Author: Samir M.
# Date: 2024-03-05
# Modified: 2024-03-05
# Description: Tokenisation
# Usage: .\02.sh

# Tokenisation:
# The process of breaking a command line into its a smaller parts called tokens using metacharacters.

# What are metacharacters?
# Metacharacters are characters that have a special meaning to the shell.
# The metacharacters are: space, tab, newline, ;, (, ), <, >, |, and &.

# The token can be either a word or an operator.
# A word doesn't contain any unquoted metacharacters.
# An operator does contain at least one unquoted metacharacters.

# Example:

name="Sam"
echo $name > file.txt
# we have 5 metacharacters here: space, > and newline.
# we have 4 tokens: echo, $name and file.txt all are words, because they don't contain any unquoted metacharacters.
# > is an operator, because it contains one unquoted metacharacter.













exit 0

0 comments on commit a86bbfc

Please sign in to comment.