Abertay Ethical Hacking Society
  • Home
  • Information
    • Constitution
    • Change Logs
      • Website
      • Discord
      • Github
      • Server
    • Meetings
      • 2021-2022
    • Honourary Members
  • Contributing
    • Contributions
      • Using Git
      • Formatting and Best Practise
  • Society Events
    • Securi-Tay
      • History
  • Help Guides
    • Programming / Scripting
      • AWK
      • Bash Scripting
      • C Coding
      • Java Coding
      • LaTeX
      • Markdown
      • Project Ideas
      • Python Scripting
      • Tools
    • Software
      • Operating Systems
        • Installing Arch
        • Installing Kali
        • Linux Commands for Beginners
        • MacOS
      • Tools
        • PGP
          • A guide to using PGP on Android
          • A guide to using PGP on macOS
          • PGP
        • Radare2
        • Nmap
        • Regular Expressions
        • The Browser Exploitation Framework (BeEF)
        • Vim
        • Vimium
        • Zsh
    • Networking
      • Domain Name System (DNS)
      • Remote access to your Abertay network drive
      • Secure Shell (SSH)
      • TLS 1.3
      • Wireshark
      • Subnetting
    • Techniques
      • A guide to creating malicious macro-enabled Excel worksheets
      • Open Source Intelligence (OSINT)
      • Google-Fu
    • Jobs
      • Common Interview Questions
    • Home Lab
      • PiHole
  • Glossary
    • Infosec Terms
    • Computing Terms
    • Hardware Terms
    • General Terms
    • Development Terms
    • Networking Terms
  • Members
    • Profiles
      • AG
      • Isaac
      • Sam
  • Other
    • Other
      • Data Dumps
      • Meetups
      • Meltdown & Spectre
      • Movies
      • Project topic suggestions
      • Recommended Reading
Powered by GitBook
On this page
  • Resources
  • Tutorials/ Getting Started
  • Reference
  • Tips & Tricks
  • Talks
  • General Advice
  • bash is not sh
  • Forget the .sh extension
  • Pipes
  • grep
  • Examples
  • Shebang
  • Execute Script
  • Debug Mode
  • Unofficial Strict Mode

Was this helpful?

  1. Help Guides
  2. Programming / Scripting

Bash Scripting

PreviousAWKNextC Coding

Last updated 3 years ago

Was this helpful?

Resources

Tutorials/ Getting Started

  • "shell scripting tutorial"

  • "aid people interested in learning to work with BASH"

Reference

  • devhints.io

Tips & Tricks

  • "a summary of everything that can, and should be done when writing shell scripts."

  • "static analysis tool for shell scripts"

  • "causes bash to behave in a way that makes many classes of subtle bugs impossible."

Talks

General Advice

bash is not sh

While bash is "sh-compatible" some features of bash will break or cause unexpected behaviour in sh.

Forget the .sh extension

Don't give scripts an .sh extension.

  1. It's a bash script, not an sh script

Pipes

grep

If you're piping to grep multiple times only the last grep in the sequence can be called with -q.

Examples

Shebang

First line of the script that indicates which interpreter is used to execute the file. The #! must be at the very start of the file, with no spaces or blank lines before it.

#!/usr/bin/env bash
    • "#!/usr/bin/env searches PATH for bash, and bash is not always in /bin, particularly on non-Linux systems."

    • "This way, you don't have to look for it in a specific place on the system, as those paths may be in different locations on different systems. As long as it's in your path, it will find it."

Execute Script

Execute bash and tell it to read the script myscript. When executing the script this way the shebang line (#!) is just a comment, bash does nothing with it

bash myscript

We can give the script executable permission. Instead of calling bash manually, we can execute myscript directly.

chmod +x myscript  # Mark myscript as executable
./myscript  # Directly execute myscript

When myscript is executed this way, the shebang line (#!) is used to determine which interpreter to use.

Debug Mode

Print every command before its execution, replacing the variables with their real values.

set -x

Unofficial Strict Mode

set -euo pipefail
# -e: Exit if any command returns non-zero status code
# -u: Prevent using undefined variables
# -o pipefail: Force pipelines to fail on first non-zero status code

by

Read for a more detailed explanation.

The advises against it unless it's a library

shellscript.sh
Linux Documentation Project - Bash Guide for Beginners
BashGuide
Serious Shell Programming
GNU Bash Reference Manual
Cheatsheet
Bash Hackers Wiki
Shell Scripts Matter
ShellCheck
Unofficial Bash Strict Mode
Google Shell Style Guide
Introduction to Advanced Bash Usage
James Pannacciulli
bash Is Not sh
Google Shell Style Guide
Rational
Rational
The Set Builtin
Unofficial Bash Strict Mode