Skip to content

Commit

Permalink
add how bash process commands
Browse files Browse the repository at this point in the history
  • Loading branch information
samirm00 committed Mar 5, 2024
1 parent dabf0ab commit f0af189
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
42 changes: 39 additions & 3 deletions 03-how-bash-process-commands/01.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash

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

Expand All @@ -11,4 +11,40 @@

# 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, | ; & ( ) < >.
# 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 f0af189

Please sign in to comment.