Bash Command Line Interpretation Steps

This section describes major processing steps, when a command line is entered at the Bash shell prompt.

When you enter a command line at the prompt, it will be processed by the Bash shell program in 5 major steps:

1. Sub-command Substitution - Any text in "`" quotes will be replaced by the output from the execution of the quoted text.

2. Filename Substitution - Any word contains character "?" will be replaced by file names that match the word with "?" as a wildcard for a single character. Any word contains character "*" will be replaced by file names that match the word with "*" as a wildcard for zero or more character.

3. Variable Substitution - Any word prefixed with "$" will be considered as a shell variable, and will be replaced by what defined in the variable.

4. Alias Substitution - If the first word matches a pre-defined alias, it will be replaced by what is defined in the alias

5. Shell Statement - If the command line match a Bash shell statement syntax, it will be executed as the shell statement. For example:

# variable assignment statement: variable_name=value
herong$ msg='hello world!' 

# conditional statement: if [...]; then ...; fi 
herong$ if [ -n "$msg" ]; then echo $msg; fi

# function statement: function_name() {...;}
herong$ greeting() { echo 'hi there!'; }

6. User-defined Function - If the first word matches a user-defined shell function, the matched function will be called to run.

7. Built-in Command - If the first word matches a shell built-in command the matched command will be executed. For example:

# change directory: cd 
herong$ cd 
herong$ cd Downloads 

# print text to output: echo
herong$ echo "Time to take a break..."
Time to take a break...

# define an alias 
alias rm='rm -i'

8. Executable Program - If the first word matches a file name of an executable program located in a directory defined in the $PATH variable, the matched program will be executed. For example,

# show $PATH directories
herong$ echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:

# run executable program at /bin/ping 
herong$ ping -V 
ping utility, iputils-s20161105

herong$ /bin/ping -V 
ping utility, iputils-s20161105

# run executable program at /usr/bin/cc 
herong$ cc --version 
cc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

herong$ /usr/bin/cc --version 
cc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

By the way, some Bash built-in commands are also available as executable programs. You can use the "type -a command" command to find out if the specified command is a built-in command or executable program, or both.

# "cd" is a built-in command 
herong$ which cd 
cd is a shell builtin

# "echo" is both a built-in command and an executable program 
herong$ type -a echo 
echo is a shell builtin
echo is /bin/echo

# "cc" is an executable program
herong$ type -a cc 
cc is /usr/bin/cc

# "greeting" is a user-defined function
herong$ type -a greeting
greeting is a function

Table of Contents

 About This Book

 Introduction to Ubuntu Systems

 GNOME - Desktop Interface and Environment

Shell - The Command-Line Interpreter

 What Is Shell

 What Is Bash (Bourne Again SHell)

 Create and Run Bash Script

Bash Command Line Interpretation Steps

 Bash Shell Session Customization

 Command Input/Output Redirection

 Shell Session Command History

 "tmux" - Terminal Multiplexer

 Process Management

 Memory Management

 Files and Directories

 APT (Advanced Package Tool)

 Network Connection on Ubuntu

 Internet Networking Tools

 SSH Protocol and ssh/scp Commands

 Administrative Tasks

 References

 Full Version in PDF/EPUB