If you’ve ever opened up the terminal on your Mac, then you may have wondered what to do next. What exactly are you supposed to type in when you see that retro-looking cursor?
While most of us control our computers with a mouse, the keyboard can also be heavily utilized to interact with it. Developers on macOS and Linux machines use Bash every day to work with files and applications and save time or make the most of limited computing resources.
In this article, we’ll go over what Bash is, what it’s used for, and how to get started with Bash.
What is Bash?
Bash is a command-line interface shell program used extensively in Linux and macOS. The name Bash is an acronym for “Bourne Again Shell,” developed in 1989 as a successor to the Bourne Shell.
“What’s a shell?” you ask? A shell is a computer program that allows you to directly control a computer’s operating system (OS) with a graphical user interface (GUI) or command-line interface (CLI).
Learn something new for free
You actually use GUI shells all the time. For example, Windows 10 is based on the Windows shell that allows you to control your OS with a desktop, taskbar, and menus.
With a CLI shell like Bash, you type commands into the program to directly control your computer’s OS. Opening up the terminal on your Mac or command line in Linux will look similar to consoles and integrated development environments (IDEs) for other programming languages such as R, Ruby, or Python. You can type commands directly in the command line or run Bash scripts to perform longer and more complex tasks.
Advantages of Bash
One of the biggest advances in computer software development was the introduction of windows and menus, so what’s the point of going back? People use CLIs like Bash because they still have a few distinct advantages over GUIs. Let’s explore some of these advantages.
Access your operating system more efficiently
People use Bash when they want to control their computer or OS without having to navigate menus, options, and windows within a GUI. As we pointed out earlier, with CLIs like Bash, you usually don’t even need to use your mouse. You can navigate through your computer’s OS without your fingers ever leaving your keyboard.
For example, if you want to quickly create, edit, or delete multiple files, it’s easier to use Bash instead of finding each file by pointing and clicking on multiple directories.
Plus, using Bash instead of a GUI is less resource-intensive since your computer doesn’t need to dedicate resources to rendering graphical output. This makes Bash an attractive option when you’re already running multiple programs, a virtual machine, or otherwise have limited computing resources to work with.
Input and output with text files
Bash makes it easy to create and edit text files, including CSVs. And, since text files are one of the most common ways to store and process data, Bash is great for performing tasks like:
- Preparing and curating data.
- Sorting and filtering through data.
- Cleaning and updating data.
Data Scientists love Bash because it helps them pre-process large datasets quickly and efficiently. And, even if you don’t need to work with big data, Bash is helpful in many other contexts. For example, two common tasks that people perform on their computers are:
- Looking up files within a folder.
- Searching for files that contain certain text.
When you do these tasks with the GUI, there’s no way to easily record your results. But with Bash, you can copy and paste your results or write them to a text file to use as input in another programming language.
Automate with ease
Bash also makes automating tasks on your computer much easier. This is especially useful when part of your job involves repetitive functions.
Suppose that you’re a cybersecurity expert at a company, and part of your job is to make sure that new hires’ laptops are configured correctly. You could use the GUI to navigate through menus and windows with points and clicks, then adjust settings as needed.
Or, you could write a single Bash script that does the same thing on any new computer. No more pointing and clicking, and no more worrying about whether you remembered to do everything on your checklist.
Heads up: We just launched new, free video-based cybersecurity courses. Start gaining foundational knowledge in cybersecurity concepts that will prepare you to pursue popular cybersecurity certifications.
Run any programming language from the same application
Whether you’re a Data Scientist or a Back-End Developer, you probably have several programming languages installed along with multiple IDE applications. Bash is a way to centralize how you run scripts in different languages so that they can effectively communicate with each other. For example, you can use Bash to:
- Run a database query using SQL
- Use the results to run an analysis in R
- Use those results as input for a Python script.
As long as the programming language is installed on your computer, you can access it with Bash.
Can you use Bash in Windows?
While Bash is famous for being the go-to CLI shell in Mac and Linux, Windows uses Command Prompt and PowerShell to perform the same functions as Bash. But, it’s now possible to install Bash on Windows 10 to provide almost the same functionality as Bash in Mac and Linux.
Most importantly, programmers who use Bash in Windows can run the same commands and utilities to query, create, edit, run, and delete files and directories.
How do you learn Bash?
Bash is a programming language as much as it is an application, which means that Bash comes with its own syntax. And, while you can look up how to use individual Bash commands as you need them, most developers find it much easier to learn all the basics at once with an introductory online course on Command Line.
To get a better understanding of the syntax, here’s a peek at what a Bash script looks like:
#!/bin/bash
echo "Counting from 1 to 5 using a for loop:"
# Simple for loop to iterate from 1 to 5
for i in {1..5}
do
echo "Number: $i"
done
echo "Finished counting."
The biggest advantage to learning Bash is that it’s so widely used. Even if you’re working in another programming language like Python or Ruby, it’s worth learning Bash because many languages support Bash commands to pass data and information to and from your computer’s OS.
5 Bash concepts to learn
We have interactive courses that’ll help you hone your Bash skills, like the beginner-friendly course Learn the Command Line. Here are five core Bash concepts that you should know about, plus courses to take to explore further:
Bash scripting
Bash scripting is a way to automate tasks in the Unix shell. For instance, it can be used to automate system maintenance tasks, like cleaning up log files or backing up data, which helps reduce manual errors and save time. You can learn how to improve your programming process with Bash scripts in the course Learn Bash Scripting.
Bash commands
Bash commands are used to perform various operations in the Unix shell, like cp
for copying files, mv
for moving files, and rm
for deleting files. Commands are essential for managing the filesystem. Bookmark our glossary of commonly used commands to have on-hand as you learn.
Bash for loop
A Bash for loop iterates over a list of items and executes a set of commands for each item. This is particularly useful for tasks like processing files in a directory or automating repetitive data processing tasks.
Bash if-else and if statements
Bash if-else and if statements are used to execute code conditionally. For example, you can write a script that checks if a file exists and performs actions based on the result. These conditional statements are crucial for creating flexible and responsive scripts.
Bash makes life easier for developers
Whether you’re pulling together data files as a Data Analyst or configuring servers as a Back-End Developer, learning Bash will make your job easier and faster. In fact, you’ll find that developers who know Bash have a head start when it comes to finding a job to start their development career.
What other skills should you have as a new developer? Check out our career paths, which have helped developers-in-training put together the right skill set to get you noticed by hiring managers.
This blog was originally published in October 2021 and has been updated to include additional courses and Bash concepts to learn.