Linux Shell Programming for Beginners
What is Shell
The shell is a command-line interpreter and scripting environment that serves as an interface between the user and the operating system’s core, known as the kernel. It allows users to interact with the system by typing human-readable commands in a terminal, which the shell then translates into instructions the kernel can understand and execute.
Shell Scripting Language
Shell scripting is a command-line programming language used to automate tasks in Unix-based systems like Linux and macOS. Common shell scripting languages are Bourne Shell (sh), GNU Bourne-Again Shell (Bash), C Shell (csh), KornShell (ksh), Z shell (Zsh), Debian Almquist shell (dash), Fish shell (fish), PowerShell (pwsh), Nushell (nu).
Script
A script is a text file that contains a sequence of commands, variables, functions, and control structures (like loops and if-else statements)
Shebang
The first line of a shell script, starting with #! (e.g., #!/bin/bash ), is called the shebang. It tells the system which interpreter to use for running the script.
Permissions
For security, Linux does not allow a file to be executed by default. You must explicitly grant execute permissions using the chmod command (e.g., chmod +x script.sh )
ls Command
The ls (list) command in Linux lists directory contents, displaying files and subdirectories. It provides detailed information such as file names, permissions, sizes, and modification dates.
The ls command syntax is:
ls [options]
| Options | Description |
|---|---|
| -l | Long format. Displays detailed file information, including permissions, ownership, size, and modification time. |
| -a | Shows all files, including hidden ones. |
| -h | Displays file sizes in human-readable formats (e.g., KB, MB) when combined with -l. |
| -R | Lists contents of directories recursively, including subdirectories. |
| -t | Sorts files by modification time, with the newest first. |
| -r | Reverses the order of the output. |
| -1 | Lists one file per line instead of displaying them in columns. |
| -d | Lists directories themselves, not their contents. |
| -L | Follows symbolic links and lists the actual file or directory they point to. |
| -X | Sorts files alphabetically by their extension. |