Shell scripting is used for automating repetitive tasks by executing a sequence of commands in a script file. You can use it for managing files, scheduling tasks, and controlling system operations. It runs in a command-line environment, processing instructions step by step. Shell scripts improve efficiency by reducing manual input and streamlining workflows.
Types of Shell Scripting
Shell scripting can be categorized as per the shell interpreter you use. Although each shell has its own set of capabilities, they all serve the same purpose: to execute commands and automate processes in Unix/Linux systems.
Bourne Shell (sh) Scripting
Choose Bourne Shell (sh) if you prefer simplicity and compatibility across Unix systems. It is efficient but lacks many advanced features found in modern shells.
Bourne Again Shell (Bash) Scripting
Bash is an improved version of Bourne Shell and acts as the default shell in most Linux distributions. You get features like command-line editing, better scripting capabilities, and improved performance.
C Shell (csh) Scripting
If you prefer C-like syntax, you might use C Shell. It offers features like aliasing and command history but is less common today due to its scripting limitations.
Korn Shell (ksh) Scripting
Korn Shell gives you better scripting capabilities and performance by combining features from both Bourne and C shells, many enterprises use it for its speed and efficiency.
Z Shell (zsh) Scripting
With Z Shell gives you advanced features like auto-completion, spelling correction, and improved customization. Developers and power users favor it for its flexibility.
Although there are multiple shell options, Bash remains the most widely used due to its flexibility and widespread support.
How Does Shell Script Work
Shell scripts work by executing a series of commands within a Unix shell, which serves as a command-line interpreter for the operating system. These scripts work involves a series of steps like writing, accessing, and executing a script.
- Write the script: Shell script consists of a series of commands written in a language that shell can interpret which also includes loops, variables, and if-then-else statements. You can also add Comments to increase readability. The first line of the script often specifies the interpreter to use (e.g., `#!/bin/bash`).
- Make the script accessible: Once the script is written, it is saved with a `.txt` or `.sh` file extension in a location accessible to the shell.
- Set and execute permissions: Before the script can be run, you must read and execute permissions for the file. Use the `chmod` command to set these permissions.
For example, `chmod +x basic_script.sh` gives the current user executable permissions.
To run a shell script in the current environment without creating a new process, use the `source` command. When the script runs other programs, these are also processes. All processes are related, and a command executed in the shell is a child process of the shell.
Shell Script Commands
Shell script commands are used to automate tasks in a Unix/Linux terminal. Here are some commonly used commands:
1. File & Directory Management
- ls – List files and directories
- pwd – Print current working directory
- cd directory_name – Change directory
- mkdir new_directory – Create a new directory
- rm file_name – Delete a file
- rm -r directory_name – Delete a directory and its contents
- cp source destination – Copy files or directories
- mv old_name new_name – Rename or move files/directories
2. File Permissions & Ownership
- chmod 755 file_name – Change file permissions
- chown user:group file_name – Change file owner
3. Process Management
- ps – View running processes
- top – Monitor system processes
4. Networking
- curl URL – Fetch data from a URL
- wget URL – Download a file
5. Text Processing
- cat file_name – Display file contents
- grep ‘text’ file_name – Search for text in a file
- sed ‘s/old/new/g’ file_name – Replace text in a file
- awk ‘{print $1}’ file_name – Extract columns from a file
Basics of Shell Scripting in Linux
Creating and Running a Shell Script
Step 1: Create a Script File
A shell script is a text file with a .sh extension.
Step 2: Add the Shebang (#!)
Every shell script starts with a shebang (#!) to specify the shell interpreter.
Step 3: Give Execute Permission
Before running the script, you need to make it executable.
Step 4: Run the Script
Execute the script using the below command:
Function with Arguments
You can pass arguments to a function when calling it.
Example: Function with One Argument
Output:
Example: Function with Multiple Arguments
Output:
Variables in Shell Scripting
Variables store data values.
Using Special Variables in Functions
- $1, $2, … – Positional parameters (arguments)
- $# – Number of arguments passed
- $@ – All arguments as separate words
- $* – All arguments as a single string
- return – Return an exit status (0 for success, nonzero for failure)
Example: Counting Arguments
Output:
Function with Conditional Statements
Output:
Shell scripting gives you the power to automate tasks, manage systems efficiently, and boost productivity in a Linux environment. By using commands, functions, and conditionals, you can simplify complex operations and save time.
FAQs
What is shell scripting used for?
Shell scripting helps in automating tasks, manages files, schedules jobs, and simplifies system administration. It facilitates the execution of multiple commands efficiently with minimal manual effort.
What is Unix and shell scripting?
Unix is an operating system known for its stability and multitasking capabilities. Shell scripting in Unix allows you to write and execute command sequences to automate processes.
Is shell scripting a language?
Shell scripting is not a standalone programming language but a way to use shell commands in a structured script. It supports programming concepts like loops, conditionals, and functions.
Is shell scripting easy?
Yes, shell scripting is relatively easy to learn, especially if you’re familiar with Linux commands. With practice, you can quickly automate tasks and improve system efficiency.