Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Core Fundamentals and Best Practices for Writing Bash Shell Scripts

Tech 2

Core Components of a Shell Script

Basic Script Structure

  • Shebang Interpreter Directive: The first line of a valid shell script, which specifies the interpreter that will execute all subsequent commands in the file. This line runs with highest priority when the script is invoked.
  • Main Program Body: Consists of executable shell commands, control flow statements, and dynamic data/variables that implement the script's core functionality.
  • Comments: Explanatory text that is fully ignored during script execution, used to document code logic, parameter meenings, and edge cases for future maintainers.

Comment Syntax in Shell Scripts

Single-line Comments

Use the # character to mark all content after it on the same line as a comment.

# Create and edit a test script
nano welcome_msg.sh

Sample content of welcome_msg.sh:

#!/bin/bash
# Print a test greeting to the terminal
echo "Hello from your custom shell script"

Run the script:

bash welcome_msg.sh
# Output:
Hello from your custom shell script
Multi-line Comments

You can use a quoted block prefixed with a colon to comment out multiple consecutive lines:

nano multi_comment_test.sh

Sample content of multi_comment_test.sh:

#!/bin/bash
: "
This is a multi-line comment block
All text contained between the two double quotes will be skipped during execution
You can add as many notes, debug records, or deprecated code snippets here as needed
"
echo "Multi-line comment test executed successfully"

Run the script:

bash multi_comment_test.sh
# Output:
Multi-line comment test executed successfully

[!NOTE] A single space is required between the leading colon : and the opening double quote " for this multi-line comment syntax to functon correctly.

Shell Script Coding Standards

Key Coding Guidelines

  • Use descriptive, purpose-specific filenames for scripts, for example mysql_incremental_backup.sh instead of generic names like test1.sh.
  • Always specify the interpreter on the first line with #!/bin/bash or #!/bin/sh to avoid inconsistent execution across different runtime environments.
  • Avoid using non-ASCII characters in comments to prevent garbled text when running the script on systems with mismatched locale settings.
  • Prioritize built-in shell commands (including echo, eval, exec, export, read, shift, exit) over external utilities to reduce process spawning overhead.
  • Minimize the number of commands used to complete a single task to improve execution efficiency. For example, avoid unnecessary pipes: Inefficient redundant pipe implementation:
    cat /etc/passwd | grep root
    # Sample output:
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    
    Optimized implemantation without redundant cat call:
    grep root /etc/passwd
    # Sample output:
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    
  • Add a standard metadata header at the top of the script to record key information:
    # Creation Date: 2024-05-20
    # Description: Performs weekly full backup of PostgreSQL databases
    # Version: 1.1.2
    # Dependencies: pg_dump, tar, awscli
    
  • Use consistent indentation to visualize code hierarchy, which greatly improves readability for later maintenance: Non-compliant code without indentation:
    #!/bin/bash
    for num in 7 8 9
    echo $num
    done
    
    Compliant code with proper indentation:
    #!/bin/bash
    for num in 7 8 9
        echo $num
    done
    

Vim Auto-Template Configuration for Shell Scripts

Manually typing the standard metadata header for every new shell script creates unnecessary repetitive work. You can configure Vim to auto-populate a pre-defined template for all new shell script files by adding the following rules to your ~/.vimrc configuration file:

set tabstop=2 autoindent expandtab
map <F3> ms:call AddShellHeader()<cr>'s
autocmd BufNewFile *.sh,*.bash call AddShellHeader()
function AddShellHeader()
    call append(0,"##################################################")
    call append(1,"# Description: ")
    call append(2,"# Version:     1.0.0")
    call append(3,"# Created On:  ".strftime("%Y-%m-%d"))
    call append(4,"# Maintainer:  engineering@example.com")
    call append(5,"# Usage:       ")
    call append(6,"##################################################")
    call append(7,"")
    call append(8,"#!/bin/bash")
endfunction
Tags: Bash Shell

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.