Dive in Shell Scripting ๐Ÿ“Ÿ

Dive in Shell Scripting ๐Ÿ“Ÿ

ยท

5 min read

Shell is the way through which we can make conversation with shell operating system. There is no GUI like windows and mac shell have Command Line Interface(CLI) through which we can run shell commands to communicate. These commands are mostly same among the different distribution of Linux. So Shell Scripting is a process in linux which is automating the day to day activities on linux computer.

Shells are usually interactive its mean they take commands from users and execute them immediately. But sometimes we need to run a bunch of commands regularly. Typing them every time in the terminal can be repetitive and time consuming.

There are Two types of Most Operating System Shell & they are GUI and CLI.

GUI make it easy to control programs using a graphical interface (GUI). They let you do things like open, close, move, and resize windows and switch between them easily Operating systems such as Windows and Ubuntu use GUIs to allow users to interact with programs visually without the need to type commands for each action. It's about making computer tasks more intuitive and user-friendly.

CLI in simple terms, a shell is a way for users to interact with a computer using text commands. On Linux/MacOS, you use a program called Terminal, and on Windows it's called Command Prompt. You type commands like "cat" or "ls" into the shell, press enter, and the computer executes them. The results of these commands are displayed in the terminal window for you to view. It's a basic but powerful way to control and use your computer.

Working with command line shell is a bit difficult for beginners as many commands are difficult to memorize. It is very powerful. It allows users to store commands in a file and execute them simultaneously. Thus any repetitive task can be easily automated. These files are commonly called batch files on Windows and shell scripts on Linux/macOS systems.

Key Elements of Shell Script

Shell keywords : Shell keywords are special words understood by the shell to control flow or perform actions. ex - if, else,break etc.

Shell commands : These are the everyday commands we use in the terminal. ex - cd to change directory, ls (to list down content, echo to display or print text, pwd to print working directory, touch to create a file, etc.

Function : Like other programming languages functions in shell scripts allow us to group commands together and reuse them.

Control flow : Shell scripts have constructs for decision making (if..then..else), looping (for, while) and handling multiple conditions (cases).

BASH (Bourne Again Shell) : This is the most widely used shell in Linux systems. It is used as the default login shell on Linux systems and on macOS. It can also be installed on Windows OS. CSH (C SHell) - The syntax and usage of the C shell is very similar to the C programming language. KSH (Korn SHell) - The Korn shell was also the basis for the POSIX shell standard specification, etc.

CSH (C Shell) : The syntax and usage of C shell is similar to C programming language. It is another shell available on Linux systems, although less used than BASH.

KSH (Korn Shell) : The Korn Shell, also known as ksh, is known for its power and influence in shaping the POSIX shell standard. It provides powerful scripting capabilities, making it preferred by some users over BASH for advanced features and efficient automation needs on Unix-based systems.

Bash (Bourne-Again SHell) : Default in many Linux distributions known for scripting and interactive use.

Sh (Bourne Shell) : Lightweight and efficient for basic scripting tasks.

Dash (Debian Almquist Shell) : defaults to /bin/sh on Debian, prioritizing speed and POSIX compliance.

Ksh (Korn Shell) : Combines features of Bourne and C shells, powerful for scripting and interactive use.

Csh (C Shell) and Tcsh (TENEX C Shell) : C-like syntax, Tcsh adds editing and completion.

Zsh (Z Shell) : Highly customizable combines features of Bash, ksh, and tcsh.

Fish (Friendly Interactive Shell) : User-friendly with syntax highlighting auto-suggest and tab completion.

We use shell scripts for several reasons few of them are

Automation : Shell scripts help us to avoid repeating the same tasks automatically.

System Administration : These are used for routine tasks like backup and system monitoring.

Adding Features : Shell scripts can add new functions to the shell to customize its behavior.

Advantages of Shell Script

Shell scripting automates tasks on Linux systems making them faster, less error and easier to customize. It's good for managing files also running system tasks and learning with lots of community support to get better.

Familiar Syntax : Shell scripting uses the same commands and syntax as typing directly on the CLI. This means we don't have to learn new rules or ways to write commands.

Rapid Development: Shell scripts are quick to write because they use existing commands and tools making automating tasks quick and clear.

Easy to Start : We can quickly start and test shell scripts in the terminal which making it easy to write and improve them.

Interactive Debugging : We can fix shell scripts step by step in the terminal which making it easy to quick to fix problems.

Disadvantages of Shell Scripts

Shell scripts may be less portable across different operating systems or shell environments, requiring adjustments for compatibility.

Error-Prone : Shell scripts can be fragile a small mistake in a command can lead to unintendentional results or errors on our system.

Slow Execution : Shell scripts compared to programs or scripts compiled in other languages shell scripts can be slow to run due to their interpreted nature.

Design Limitations : Shell scripting may not have as many advanced features as other scripting languages, which may make it less ideal for handling complex task.

Overall, shell scripts are great for quick and simple tasks on the command line, but they can be less reliable and efficient for complex, large-scale applications.

Difference between /bin/sh & /bin/bash !!

Okay, previously both of them were same because /bin/sh was redirecting using the linking concept to /bin/bash but now it is not same because some of the operating system have decided to use dash by default so our might not execute if we bash scripting on machine where dash is default.

For a long time, /bin/sh was linked to /bin/bash. As time went on, bash developed more features and extensions, some of which didn't adhere to the POSIX standards. Consequently, it was no longer an option on most Linux distros, and other POSIX compliant shells replaced it. by google.com

Did you find this article valuable?

Support dailydoseindevops by becoming a sponsor. Any amount is appreciated!