What is Bash Scripting?
The Bourne-Again SHell (Bash) was developed by the Free Software Foundation (FSF) under the GNU Project, which gives it a somewhat special reputation within the Open Source community. Today, Bash is the default user shell on most Linux installations. Although Bash is just one of several well known UNIX shells, its wide distribution with Linux makes it an important tool to know.
The main purpose of a UNIX shell is to allow users to interact effectively with the system through the command line. A common shell action is to invoke an executable, which in turn causes the kernel to create a new running process. Shells have mechanisms to send the output of one program as input into another and facilities to interact with the filesystem. For example, a user can traverse the filesystem or direct the output of a program to a file.
Although Bash is primarily a command interpreter, it's also a programming language. Bash supports variables, functions and has control flow constructs, such as conditional statements and loops. However, all of this comes with some unusual quirks. This is because Bash attempts to fulfill two roles at the same time: to be a command interpreter and a programming language—and there is tension between the two.
All UNIX shells, including Bash, are primarily command interpreters. This trait has a deep history, stretching all the way to the very first shell and the first UNIX system. Over time, UNIX shells acquired the programming capabilities by evolution, and this has led to some unusual solutions for the programming environment. As many people come to Bash already having some background in traditional programming languages, the unusual perspective that Bash takes with programming constructs is a source of much confusion, as evidenced by many questions posted on Bash forums.
Bash vs. Python: Which language should you use?
Both programming languages have pros and cons that make them better for some tasks than others.
Bash and Python are most automation engineers' favorite programming languages. Both have pros and cons, and sometimes it can be hard to choose which one you should use. The honest answer is: It depends on the task, the scope, the context, and the complexity of the task.
Let's compare these two languages to get a better understanding of where each one shines.
- Is a Linux/Unix shell command language
- Is great for writing shell scripts that use command line interface (CLI) utilities, utilizing output from one command to another (piping), and executing simple tasks (up to 100 lines of code)
- Can utilize command-line commands and utilities as-is
- Has better startup time than Python but poor execution time performance
- Does not come preinstalled in Windows; your script might not be compatible with multiple operating systems, but Bash is the default shell on most Linux/Unix systems
- Is not fully compatible with other shells (e.g., csh, zsh, fish)
- Piping ("|") CLI utilities like sed, awk, grep, etc. can slow its performance
- Lacks many functions, objects, data structures, and multi-threading, which limits its use for complex scripting/programming
- Lacks good debugging tools and utilities
- Is an object-oriented programming (OOP) language, so it's more general purpose than Bash
- Can be used for almost any task
- Works on most major operating systems and is also installed by default on most Unix/Linux systems
- Is very similar to writing pseudo code
- Has simple, clear, easy-to-learn, and easy-to-read syntax
- Has lots of libraries, documentation, and an active community
- Provides better error handling features than Bash
- Has better debugging tools and utilities than Bash, which makes it a great language for developing complex software applications involving many lines of code
- Applications (or scripts) can contain many third-party dependencies that must be installed before executing them
- Requires writing more lines of code for simple tasks than Bash does
I hope these lists give you a better understanding of which language to use and when to use it.
Select the language of your preference