Introduction to Linux: POSIX and Bash Syntax                              
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                            Philipp Fruck & Pius Walter                                                            
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                            POSIX vs Bash                                                             
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ Goal of this lecture                                          ██ Use Cases:                                                         
                                                                                                                                                   
               •  Learn core Bash scripting syntax                              •  Portable automation scripts                                     
               •  Understand differences between POSIX and                      •  Configuration and setup scripts                                 
                  Bash-specific features                                        •  Writing reusable shell functions                                
                                                                                                                                                   
            ██ Why?                                                                                                                                
                                                                                                                                                   
               •  Scripts need to run reliably across different                                                                                    
                  shells                                                                                                                           
               •  Core syntax knowledge is the foundation for writing                                                                              
                  scripts                                                                                                                          
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                             2 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                            POSIX vs Bash                                                             
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            POSIX is a standard, Bash is a shell implementation                                                                                    
                                                                                                                                                   
               •  /bin/sh -> POSIX-compliant shell                                                                                                 
               •  /bin/bash -> Bash-specific features, not always POSIX                                                                            
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                             3 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                            POSIX vs Bash                                                             
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            POSIX is a standard, Bash is a shell implementation                                                                                    
                                                                                                                                                   
               •  /bin/sh -> POSIX-compliant shell                                                                                                 
               •  /bin/bash -> Bash-specific features, not always POSIX                                                                            
                                                                                                                                                   
            ▍ On Fedora, /bin/sh is a symlink to /bin/bash, but other distros like Debian ship more minimal shells like dash                       
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                             4 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                            POSIX vs Bash                                                             
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            POSIX is a standard, Bash is a shell implementation                                                                                    
                                                                                                                                                   
               •  /bin/sh -> POSIX-compliant shell                                                                                                 
               •  /bin/bash -> Bash-specific features, not always POSIX                                                                            
                                                                                                                                                   
            ▍ On Fedora, /bin/sh is a symlink to /bin/bash, but other distros like Debian ship more minimal shells like dash                       
                                                                                                                                                   
            POSIX standard includes:                                                                                                               
                                                                                                                                                   
               •  Standard shell syntax (if, while, for, case)                                                                                     
               •  Conditionals ([ ... ], with =, -f, -n, etc.)                                                                                     
               •  Variables and expansions                                                                                                         
               •  Redirection                                                                                                                      
               •  Command substitution                                                                                                             
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                             5 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                            POSIX vs Bash                                                             
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            POSIX is a standard, Bash is a shell implementation                                                                                    
                                                                                                                                                   
               •  /bin/sh -> POSIX-compliant shell                                                                                                 
               •  /bin/bash -> Bash-specific features, not always POSIX                                                                            
                                                                                                                                                   
            ▍ On Fedora, /bin/sh is a symlink to /bin/bash, but other distros like Debian ship more minimal shells like dash                       
                                                                                                                                                   
            POSIX standard includes:                                                                                                               
                                                                                                                                                   
               •  Standard shell syntax (if, while, for, case)                                                                                     
               •  Conditionals ([ ... ], with =, -f, -n, etc.)                                                                                     
               •  Variables and expansions                                                                                                         
               •  Redirection                                                                                                                      
               •  Command substitution                                                                                                             
                                                                                                                                                   
            Bash adds arrays, enhanced conditionals ([[ ... ]]) and some more syntax.                                                              
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                             6 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                            POSIX vs Bash                                                             
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            POSIX is a standard, Bash is a shell implementation                                                                                    
                                                                                                                                                   
               •  /bin/sh -> POSIX-compliant shell                                                                                                 
               •  /bin/bash -> Bash-specific features, not always POSIX                                                                            
                                                                                                                                                   
            ▍ On Fedora, /bin/sh is a symlink to /bin/bash, but other distros like Debian ship more minimal shells like dash                       
                                                                                                                                                   
            POSIX standard includes:                                                                                                               
                                                                                                                                                   
               •  Standard shell syntax (if, while, for, case)                                                                                     
               •  Conditionals ([ ... ], with =, -f, -n, etc.)                                                                                     
               •  Variables and expansions                                                                                                         
               •  Redirection                                                                                                                      
               •  Command substitution                                                                                                             
                                                                                                                                                   
            Bash adds arrays, enhanced conditionals ([[ ... ]]) and some more syntax.                                                              
                                                                                                                                                   
            ▍ The shellcheck utility allows linting your shell script.                                                                             
            ▍ It determines the correct shell syntax using the shebang.                                                                            
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                             7 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                  Shebang                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ How-to                                                                             ██ Examples                                      
                                                                                                                                                   
               •  The shebang line tells the system which interpreter should execute the                                                           
                  script.                                                                           #!/bin/sh                                      
               •  Must appear as the first line of the script                                                                                      
               •  Starts with #! followed by the interpreter path                                                                                  
               •  Allows running ./script instead of bash ./script                                                                                 
                  ◦  Ensures correct interpreter is used even if no file extension is               #!/bin/bash                                    
                     present                                                                                                                       
                  ◦  Don't forget to chmod +x ./script (details in the permissions                                                                 
                     lecture)                                                                                                                      
                                                                                                    #!/usr/bin/env bash                            
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                    #!/usr/bin/env python3                         
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                             8 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                  Shebang                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ How-to                                                                             ██ Examples                                      
                                                                                                                                                   
               •  The shebang line tells the system which interpreter should execute the                                                           
                  script.                                                                           #!/bin/sh                                      
               •  Must appear as the first line of the script                                                                                      
               •  Starts with #! followed by the interpreter path                                                                                  
               •  Allows running ./script instead of bash ./script                                                                                 
                  ◦  Ensures correct interpreter is used even if no file extension is               #!/bin/bash                                    
                     present                                                                                                                       
                  ◦  Don't forget to chmod +x ./script (details in the permissions                                                                 
                     lecture)                                                                                                                      
                                                                                                    #!/usr/bin/env bash                            
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                    #!/usr/bin/env python3                         
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ▍ 💡 Tip: Using /usr/bin/env makes your script more portable across systems (/bin/sh is a special case as it is required by             
            ▍ the POSIX standard). Use of #!/bin/bash, #!/usr/bin/python etc. is discouraged.                                                      
                                                                                                                                                   
            ▍ More advanced: Watch the first minutes of https://media.ccc.de/v/all-systems-go-2026-440-binfmtbpf (especially p. 13-14)             
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                             9 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                          POSIX Variables                                                           
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                     1 # Assign a variable. Use quotes to include spaces                                                                           
                     2 name=Alice                                                                                                                  
                     3 fullname="Bob the builder"                                                                                                  
                     4                                                                                                                             
                     5 # Use a variable with $ or use brackets to define                                                                           
                       the start and end of variables                                                                                              
                     6 echo "Hello $name"                                                                                                          
                     7 echo "${name}.lastname@example.com"                                                                                         
                     8                                                                                                                             
                     9 # Read-only                                                                                                                 
                    10 readonly pi=3.14                                                                                                            
                    11                                                                                                                             
                    12 # Per command vs global environment variables                                                                               
                    13 foo=bar mylocalscript                                                                                                       
                    14 export PATH="/usr/local/bin:$PATH"                                                                                          
                                                                                                                                                   
                                                                                                                                                   
            ▍ Note: Every variable is a string                                                                                                     
                                                                                                                                                   
            ▍ Be careful with special variables like PATH and PS1                                                                                  
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            10 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                          POSIX Variables                                                           
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                     1 # Assign a variable. Use quotes to include spaces                   1 readonly name=Bob                                     
                     2 name=Alice                                                          2 fullname="$name the builder"                          
                     3 fullname="Bob the builder"                                          3 # Variables are replaced in double                    
                     4                                                                       quotes                                                
                     5 # Use a variable with $ or use brackets to define                   4 echo "Fullname: $fullname"                            
                       the start and end of variables                                      5 # But not in single quotes:                           
                     6 echo "Hello $name"                                                  6 message='$name is a nice name!'                       
                     7 echo "${name}.lastname@example.com"                                 7 echo $message                                         
                     8                                                                     8 # readonly cannot be changed                          
                     9 # Read-only                                                         9 name="Beep 🤖"                                         
                    10 readonly pi=3.14                                                                                                            
                    11                                                                                                                             
                    12 # Per command vs global environment variables                     ——————————— [finished with error] ———————————             
                    13 foo=bar mylocalscript                                                                                                       
                    14 export PATH="/usr/local/bin:$PATH"                                                                                          
                                                                                           Fullname: Bob the builder                               
                                                                                           $name is a nice name!                                   
            ▍ Note: Every variable is a string                                             /home/runner/work/_temp/nix-shell.0FChq9/               
                                                                                           .presentermaBvnBb/script.sh: 9: name: is                
            ▍ Be careful with special variables like PATH and PS1                          read only                                               
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            11 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                          POSIX Variables                                                           
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                     1 # Assign a variable. Use quotes to include spaces                   1 readonly name=Bob                                     
                     2 name=Alice                                                          2 fullname="$name the builder"                          
                     3 fullname="Bob the builder"                                          3 # Variables are replaced in double                    
                     4                                                                       quotes                                                
                     5 # Use a variable with $ or use brackets to define                   4 echo "Fullname: $fullname"                            
                       the start and end of variables                                      5 # But not in single quotes:                           
                     6 echo "Hello $name"                                                  6 message='$name is a nice name!'                       
                     7 echo "${name}.lastname@example.com"                                 7 echo $message                                         
                     8                                                                     8 # readonly cannot be changed                          
                     9 # Read-only                                                         9 name="Beep 🤖"                                         
                    10 readonly pi=3.14                                                                                                            
                    11                                                                                                                             
                    12 # Per command vs global environment variables                     ——————————— [finished with error] ———————————             
                    13 foo=bar mylocalscript                                                                                                       
                    14 export PATH="/usr/local/bin:$PATH"                                                                                          
                                                                                           Fullname: Bob the builder                               
                                                                                           $name is a nice name!                                   
            ▍ Note: Every variable is a string                                             /home/runner/work/_temp/nix-shell.0FChq9/               
                                                                                           .presentermaBvnBb/script.sh: 9: name: is                
            ▍ Be careful with special variables like PATH and PS1                          read only                                               
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ▍ Variable scoping: local variables inside functions vs global outside.                                                                
                                                                                                                                                   
            ▍ Using export allows variables to be used by other processes launched from the current session.                                       
                                                   Introduction to Linux: POSIX and Bash Syntax                                            12 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                       POSIX Conditionals                                                        
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ If-else logic                                                                                                                       
                                                                                                                                                   
            The if statement runs commands conditionally based on                                                                                  
            exit status. Do not forget spaces around [ and ].                                                                                      
                                                                                                                                                   
                                                                                                                                                   
                # hint: Do not use this in prod :)                                                                                                 
                if [ "$USER" = "root" ]; then                                                                                                      
                    echo "Welcome, root user!"                                                                                                     
                elif [ "$USER" = "admin" ]; then                                                                                                   
                    echo "Hello, admin."                                                                                                           
                else                                                                                                                               
                    echo "Access denied."                                                                                                          
                fi                                                                                                                                 
                                                                                                                                                   
                # negation works with != or ! in beginning                                                                                         
                if [ ! "$USER" = "root" ]; then echo "No!"; fi                                                                                     
                if ! grep MyText myfile.txt; then echo Nope; fi                                                                                    
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            13 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                       POSIX Conditionals                                                        
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ If-else logic                                                                                                                       
                                                                                                                                                   
            The if statement runs commands conditionally based on                                                                                  
            exit status. Do not forget spaces around [ and ].                                                                                      
                                                                                                                                                   
                                                                                                                                                   
                # hint: Do not use this in prod :)                                                                                                 
                if [ "$USER" = "root" ]; then                                                                                                      
                    echo "Welcome, root user!"                                                                                                     
                elif [ "$USER" = "admin" ]; then                                                                                                   
                    echo "Hello, admin."                                                                                                           
                else                                                                                                                               
                    echo "Access denied."                                                                                                          
                fi                                                                                                                                 
                                                                                                                                                   
                # negation works with != or ! in beginning                                                                                         
                if [ ! "$USER" = "root" ]; then echo "No!"; fi                                                                                     
                if ! grep MyText myfile.txt; then echo Nope; fi                                                                                    
                                                                                                                                                   
                                                                                                                                                   
            Concatenation (if ...; then is optional)                                                                                               
                                                                                                                                                   
                                                                                                                                                   
              [ "$USER" = "root" ] || [ "$USER" = "admin" ] && echo                                                                                
              "Woohoo"                                                                                                                             
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            14 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                       POSIX Conditionals                                                        
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ If-else logic                                                 ██ Common test conditions                                             
                                                                                                                                                   
            The if statement runs commands conditionally based on            Operator │ Meaning                       │ Example                    
            exit status. Do not forget spaces around [ and ].                                                                                      
                                                                             ─────────┼───────────────────────────────┼───────────────             
                                                                             ───                                                                   
                # hint: Do not use this in prod :)                           -e       │ Path exists                   │ [ -e "$file" ]             
                if [ "$USER" = "root" ]; then                                                                                                      
                    echo "Welcome, root user!"                               -f       │ Regular file exists           │ [ -f "$file" ]             
                elif [ "$USER" = "admin" ]; then                                                                                                   
                    echo "Hello, admin."                                     -d       │ Directory exists              │ [ -d "$dir" ]              
                else                                                                                                                               
                    echo "Access denied."                                    -r       │ Path is readable              │ [ -r "$file" ]             
                fi                                                                                                                                 
                                                                             -w       │ Path is writable              │ [ -w "$file" ]             
                # negation works with != or ! in beginning                                                                                         
                if [ ! "$USER" = "root" ]; then echo "No!"; fi               -x       │ Path is executable/searchable │ [ -x "$file" ]             
                if ! grep MyText myfile.txt; then echo Nope; fi                                                                                    
                                                                             -s       │ File exists and is non-empty  │ [ -s "$file" ]             
                                                                                                                                                   
            Concatenation (if ...; then is optional)                         -L       │ Path is a symbolic link       │ [ -L "$file" ]             
                                                                                                                                                   
                                                                             -z       │ String is empty               │ [ -z "$var" ]              
              [ "$USER" = "root" ] || [ "$USER" = "admin" ] && echo                                                                                
              "Woohoo"                                                       -n       │ String is non-empty           │ [ -n "$var" ]              
                                                                                                                                                   
                                                                             =        │ Strings are equal             │ [ "$a" = "$b"              
                                                   Introduction to Linux: POSIX and Bash Syntax                                            15 / 24 
                                                                             !=       │ Strings are different         │ [ "$a" != "$b"             
                                                                             ]                                                                     
                                                                                                                                                   
                                                                                                                                                   
                                               Bash-Specific Conditionals                                                
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ Safer comparison                                                                                                                    
                                                                                                                                                   
            Bash's [[ ... ]] supports safer string comparisons (==) that prevent                                                                   
            word splitting or path name expansion and logical operators.                                                                           
                                                                                                                                                   
                                                                                                                                                   
                       if [[ "$USER" == "root" ]]; then                                                                                            
                           echo "Welcome, root user!"                                                                                              
                       elif [[ "$USER" == admin* ]]; then                                                                                          
                           echo "Hello, admin."                                                                                                    
                       else                                                                                                                        
                           echo "Access denied."                                                                                                   
                       fi                                                                                                                          
                                                                                                                                                   
                       [[ "$USER" != "admin" ]] && echo "no!"                                                                                      
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            16 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                               Bash-Specific Conditionals                                                
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ Safer comparison                                                                                                                    
                                                                                                                                                   
            Bash's [[ ... ]] supports safer string comparisons (==) that prevent                                                                   
            word splitting or path name expansion and logical operators.                                                                           
                                                                                                                                                   
                                                                                                                                                   
                       if [[ "$USER" == "root" ]]; then                                                                                            
                           echo "Welcome, root user!"                                                                                              
                       elif [[ "$USER" == admin* ]]; then                                                                                          
                           echo "Hello, admin."                                                                                                    
                       else                                                                                                                        
                           echo "Access denied."                                                                                                   
                       fi                                                                                                                          
                                                                                                                                                   
                       [[ "$USER" != "admin" ]] && echo "no!"                                                                                      
                                                                                                                                                   
                                                                                                                                                   
            Combine tests using && and ||:                                                                                                         
                                                                                                                                                   
                                                                                                                                                   
                       if [[ -f "$file" && -r "$file" ]]; then                                                                                     
                           echo "File exists and is readable."                                                                                     
                       fi                                                                                                                          
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            17 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                               Bash-Specific Conditionals                                                
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ Safer comparison                                                          ▓▓▓ Regex matching                                        
                                                                                                                                                   
            Bash's [[ ... ]] supports safer string comparisons (==) that prevent         Use =~ to match a POSIX extended regular                  
            word splitting or path name expansion and logical operators.                 expression (man 7 regex).                                 
                                                                                                                                                   
                                                                                         BASH_REMATCH is an array: [0] holds the whole             
                       if [[ "$USER" == "root" ]]; then                                  match, [1], [2], ... the capture groups.                  
                           echo "Welcome, root user!"                                                                                              
                       elif [[ "$USER" == admin* ]]; then                                                                                          
                           echo "Hello, admin."                                            input=abc123                                            
                       else                                                                if [[ "$input" =~ [0-9]+ ]]; then                       
                           echo "Access denied."                                               echo "Found number: $BASH_REMATCH"                  
                       fi                                                                  else                                                    
                                                                                               echo "Invalid input."                               
                       [[ "$USER" != "admin" ]] && echo "no!"                              fi                                                      
                                                                                                                                                   
                                                                                                                                                   
            Combine tests using && and ||:                                               ———————————————— [finished] —————————————————             
                                                                                                                                                   
                                                                                                                                                   
                       if [[ -f "$file" && -r "$file" ]]; then                             Found number: 123                                       
                           echo "File exists and is readable."                                                                                     
                       fi                                                                                                                          
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            18 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                               Arithmetic                                                                
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ Bash                                                                                                                                
                                                                                                                                                   
            Bash supports (( ... )) for arithmetic comparisons.                                                                                    
                                                                                                                                                   
            A nonzero arithmetic result means success.                                                                                             
                                                                                                                                                   
                                                                                                                                                   
                       count="50"                                                                                                                  
                       if (( count > 10 && count < 100 )); then                                                                                    
                           echo "Count is in range."                                                                                               
                       fi                                                                                                                          
                                                                                                                                                   
                       if (( count % 2 == 0 )); then                                                                                               
                           echo "Even number."                                                                                                     
                       fi                                                                                                                          
                                                                                                                                                   
                       ((count++))                                                                                                                 
                       echo Count: $count                                                                                                          
                                                                                                                                                   
                       (( "1" > 2 )) || echo false                                                                                                 
                                                                                                                                                   
                                                                                                                                                   
                     ——————————————————— [finished] ———————————————————                                                                            
                                                                                                                                                   
                                                                                                                                                   
                       Count is in range.                                                                                                          
                       Even number.                                                                                                                
                       Count: 51                   Introduction to Linux: POSIX and Bash Syntax                                            19 / 24 
                       false                                                                                                                       
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                               Arithmetic                                                                
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ Bash                                                                      ██ POSIX                                                  
                                                                                                                                                   
            Bash supports (( ... )) for arithmetic comparisons.                          POSIX only supports arithmetic via $(( ... )).            
                                                                                                                                                   
            A nonzero arithmetic result means success.                                   Results are passed as string (no exit code).              
                                                                                                                                                   
                                                                                                                                                   
                       count="50"                                                          # in POSIX, numbers must not be quoted!                 
                       if (( count > 10 && count < 100 )); then                            echo $(( 1 + 2 ))                                       
                           echo "Count is in range."                                       echo -n $(( 1 > 2 )) && echo '->yes'                    
                       fi                                                                  echo -n $(( 1 < 2 )) && echo '->yes'                    
                                                                                                                                                   
                       if (( count % 2 == 0 )); then                                       counter=0                                               
                           echo "Even number."                                             # POSIX-compliant increment                             
                       fi                                                                  counter=$(( counter + 1 ))                              
                                                                                           echo Counter: $counter                                  
                       ((count++))                                                                                                                 
                       echo Count: $count                                                                                                          
                                                                                         ———————————————— [finished] —————————————————             
                       (( "1" > 2 )) || echo false                                                                                                 
                                                                                                                                                   
                                                                                           3                                                       
                     ——————————————————— [finished] ———————————————————                    0->yes                                                  
                                                                                           1->yes                                                  
                                                                                           Counter: 1                                              
                       Count is in range.                                                                                                          
                       Even number.                                                                                                                
                       Count: 51                   Introduction to Linux: POSIX and Bash Syntaxw do we check $(( 1 > 2 )) in POSIX?        20 / 24 
                       false                                                                                                                       
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                               Selections                                                                
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ POSIX Case statement                                                                                                                
                                                                                                                                                   
            case compares a value against multiple patterns                                                                                        
                                                                                                                                                   
                                                                                                                                                   
                 case "$arg" in                                                                                                                    
                     start)                                                                                                                        
                         echo "Starting service..."                                                                                                
                         ;;                                                                                                                        
                     stop)                                                                                                                         
                         echo "Stopping service..."                                                                                                
                         ;;                                                                                                                        
                     restart|reload)                                                                                                               
                         echo "Restarting service..."                                                                                              
                         ;;                                                                                                                        
                     *)                                                                                                                            
                         echo "Usage: $0 {start|stop|restart}"                                                                                     
                         exit 1                                                                                                                    
                         ;;                                                                                                                        
                 esac                                                                                                                              
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            21 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                               Selections                                                                
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ POSIX Case statement                                          ██ Bash Select                                                        
                                                                                                                                                   
            case compares a value against multiple patterns                                                                                        
                                                                                  select choice in "foo" "bar" "foobar"; do                        
                                                                                      echo "choice: $choice, reply: $REPLY"                        
                 case "$arg" in                                                       break                                                        
                     start)                                                       done                                                             
                         echo "Starting service..."                                                                                                
                         ;;                                                                                                                        
                     stop)                                                                                                                         
                         echo "Stopping service..."                                                                                                
                         ;;                                                                                                                        
                     restart|reload)                                                                                                               
                         echo "Restarting service..."                                                                                              
                         ;;                                                                                                                        
                     *)                                                                                                                            
                         echo "Usage: $0 {start|stop|restart}"                                                                                     
                         exit 1                                                                                                                    
                         ;;                                                                                                                        
                 esac                                                                                                                              
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            22 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                               Selections                                                                
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            ██ POSIX Case statement                                          ██ Bash Select                                                        
                                                                                                                                                   
            case compares a value against multiple patterns                                                                                        
                                                                                  select choice in "foo" "bar" "foobar"; do                        
                                                                                      echo "choice: $choice, reply: $REPLY"                        
                 case "$arg" in                                                       break                                                        
                     start)                                                       done                                                             
                         echo "Starting service..."                                                                                                
                         ;;                                                                                                                        
                     stop)                                                   ▓▓▓ Combining select and case                                         
                         echo "Stopping service..."                                                                                                
                         ;;                                                                                                                        
                     restart|reload)                                              PS3="Choose an option: "                                         
                         echo "Restarting service..."                             select choice in "Date" "List" "Quit"; do                        
                         ;;                                                           case $REPLY in                                               
                     *)                                                                   1) date ;;                                               
                         echo "Usage: $0 {start|stop|restart}"                            2) ls -l ;;                                              
                         exit 1                                                           3) echo "Goodbye!"; break ;;                             
                         ;;                                                               *) echo "Invalid choice." ;;                             
                 esac                                                                 esac                                                         
                                                                                  done                                                             
                                                                                                                                                   
                                                                                                                                                   
                                                                             ▍ select uses PS3 instead of PS1.                                     
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            23 / 24 
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                               Thank you!                                                                
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
            Thank you for your attention!                                                                                                          
                                                                                                                                                   
            Don't forget the feedback in Moodle please!                                                                                            
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                                                                                                                   
                                                   Introduction to Linux: POSIX and Bash Syntax                                            24 / 24