Shell Scripting Fundamentals: Creation, Permissions, and Execution
A shell script is an automated sequence of terminal commands stored in a plain text file, executed sequentially by the operating system's shell interpreter. This chapter covers the foundational mechanics of writing shell scripts, configuring Linux file permissions using octal notation via chmod, executing scripts safely in the terminal environment, and establishing the core programming constructs required for DevOps automation.
1. What is a Shell Script? (The Movie Script Mental Model)
At its core, a shell script is simply a collection of Linux commands written line-by-line in a text file. Instead of typing individual commands manually into the terminal prompt, you record them inside a script so the shell can execute them in sequence.
Real-Life Analogy
Think of a shell script like a movie script. In a movie script, actor A speaks a dialogue, then actor B responds with another dialogue, followed by actor C. The performance proceeds sequentially from line 1 to the end. Similarly, in a shell script, command 1 executes, followed immediately by command 2, and then command 3, producing an automated end-to-end workflow.
In Linux scripting:
- Lines starting with
#are comments meant for human documentation. The shell skips them during execution. - System commands like
echoprint output directly to standard output (STDOUT).
2. Writing Your First Shell Script (hello.sh)
To write a shell script, create a file ending with the .sh extension using a terminal text editor such as vim or nano.
# Open or create a script file using Vim
vim hello.sh
Inside the file, write your sequence of shell commands line by line:
# This is a script for TrainWithShubham (TWS) shell scripting tutorial
# Comments are ignored by the shell interpreter
echo "TWS: Hello everyone!"
echo "Learners: DevOps engineer here, we will definitely comment!"
echo "TWS: Guys, make sure to like the video as well!"
To save and exit in Vim:
- Press
Escto return to Normal mode. - Type
:wq(Write and Quit). - Press
Enter.
3. Understanding Linux File Permissions (ls -l)
When you create a new file in Linux, it is created as a non-executable text file by default for security reasons. Attempting to run it directly without permission will result in a "Permission Denied" error.
To inspect the permissions assigned to your script, run the long listing command:
ls -l hello.sh
Breakdown of Permission Output
A typical permission string looks like this:
-rw-r--r-- 1 user group 120 Oct 24 10:00 hello.sh
The 10-character permission block (-rw-r--r--) breaks down into four distinct segments:
Notice that none of the sections contain an x (execute) character. To run the file as a script, explicit execution permissions must be granted.
4. Permission Math: Decoding Octal Notation (chmod)
Permissions in Linux are modified using the chmod command (Change Mode). Permissions can be expressed numerically using Octal Notation, where each permission type is assigned a distinct bit weight:
- Read () =
- Write () =
- Execute () =
Calculating Permission Sums
To compute a target permission level, add the values corresponding to the desired capabilities:
- Read + Write + Execute () =
- Read + Execute () =
- Read + Write () =
- Read Only () =
- No Permissions () =
Walkthrough of chmod 754
If you apply the octal mode 754 to hello.sh:
chmod 754 hello.sh
The permissions are applied across the three user categories as follows:
- Owner/User (): Read, Write, and Execute (
rwx) - Group (): Read and Execute (
r-x) - Others (): Read Only (
r--)
Note: If you want standard execution access restricted strictly to your user without group/other execution,
chmod 700 hello.shorchmod +x hello.shis commonly used.
5. Applying Permissions and Terminal Color Indicators
After executing the chmod command, running ls -l reveals updated file attributes and terminal visual cues:
$ chmod 754 hello.sh
$ ls -l hello.sh
-rwxr-xr-- 1 user group 120 Oct 24 10:05 hello.sh
Most modern terminal environments (such as Bash on Ubuntu or macOS) automatically color-code file names based on their attributes and type.
6. Executing the Script (./hello.sh)
Once execution permissions are granted (turning the file name green), you run the script directly from your terminal.
./hello.sh
Why is ./ Required?
In Linux, the system searches for executable binaries inside directories listed in the global $PATH environment variable (e.g., /usr/bin, /bin). Because the current working directory (.) is omitted from $PATH for security reasons, pre-pending ./ explicitly instructs the shell: "Look in the current directory for this file."
Execution Flow Walkthrough
Terminal Output
$./hello.sh
TWS: Hello everyone!
Learners: DevOps engineer here, we will definitely comment!
TWS: Guys, make sure to like the video as well!
7. The DevOps Shell Scripting Core Roadmap
Creating basic sequential scripts forms the groundwork. To build production-grade automation pipelines (e.g., CI/CD deployment scripts, health check utilities, log analyzers), a DevOps engineer must master five fundamental scripting constructs:
8. Summary and Key Takeaways
- Script Definition: A shell script is a text file containing an ordered list of shell commands executed sequentially.
- Comments (
#): Used for code documentation; ignored completely by the shell interpreter during execution. - File Security Default: Newly created text files lack execute permissions (
x) by default to prevent unauthorized command execution. - Permission Checking: Use
ls -lto inspect current file permission strings across User, Group, and Others. - Octal Bit Math: Permissions use bit values where Read = , Write = , and Execute = .
- Permission Assignment: The
chmod 754 script.shcommand grants full permissions () to the owner, read/execute () to the group, and read-only () to others. - Terminal Visual Indicators: Executable files appear green in standard Linux terminal output, plain files appear white, and directories appear blue.
- Script Invocation: Prefix execution commands with
./(e.g.,./hello.sh) to target files in the current working directory explicitly. - DevOps Scripting Roadmap: Production automation relies on mastering five key pillars: Variables, Arguments, Conditionals, Loops, and Functions.