Home Learn Linux Bash shell in Linux: A dive into command-line excellence

Bash shell in Linux: A dive into command-line excellence

The Bash shell is a fundamental component of Linux systems, serving as the default command-line interface for many distributions. It allows users to execute commands, write scripts for automation, and manage system operations efficiently. Understanding Bash is essential for navigating and harnessing the full potential of Linux environments.

by Pulkit Chandak
Published: Last Updated on
what is bash shell,

If you have ever seen any movie or series with a “hacker” character (a genuinely good example would be Mr. Robot), you have seen this scene. There is a bunch of random text on the screen, the hacker puts in some command, and the screen pours out more information. So what’s that all about? Why are there no icons or anything graphical? Well, I am here with an answer.

Command Line Interface

What is being shown in a scene like this is a Command Line Interface (CLI). The CLI of any operating system makes it possible for mere mortals like us to interact with the complex systems of our machines. We enter commands in the form that we understand them. Next, they are sent to the shell, the software that makes sense of the commands, variables, and names that we put in. Finally, the command is executed, and we are provided with the results.

In simple words, a shell provides an interface to the operating system.

This article will talk about one particular shell, namely Bash. Bash is the shell that is used most widely among the Linux distributions. It is the default login shell for most Linux distributions. Therefore, when you see any Linux terminal commands anywhere, they mostly refer to the Bash shell. That said, let us get into its history.

History of Bash

First came the Thompson shell

The Thompson shell came with the first release of Unix in 1971. Ken Thompson wrote it, and it was only a simple command interpreter. The shell was very compartmentalized. The feature that helps the shell identify filenames using patterns was separate from the shell in a script called glob. Even the if command to evaluate conditional statements differed from the main shell. As a result, the shell came in below 900 lines of C code.

But the shell included many features still found in modern shells. The method of redirecting information (example: | or >) flow and sequencing multiple commands in a single line (example: semicolon[;] or &&) have still survived.

The main shortcoming of the Thompson shell was the lack of scripting. You could sit and type in commands all day long, but you couldn’t write a script that could run a sequence of commands with the execution of a single file.

Next came the Bourne shell

The Bourne shell was released to accompany Unix 7. Stephen Bourne developed it and introduced several improvements over the older Thompson shell. The Bourne shell most notably introduced variables, control flows, and loops. It provided the ability to create scripts as well. The only lack that the Bourne shell had was the inability to develop functions.

And thus the shell was Bourne again

Finally, as a part of the GNU Project (the GNU project was created by Richard Stallman to provide quality software, like Unix, for free for everyone to use), the Bourne shell was recreated as the Bourne-Again Shell or our dear Bash. It was developed finally by Brain Fox, who made the brilliant choice to name it after a pun and not himself.

Since its creation in 1988, Bash has been adapted to most Linux distributions. Apple has even adopted it in their Mac OS Catalina and adapted it to Microsoft Windows. Bash has been in development, being constantly improved upon, and is used continuously by users worldwide.

Why is Bash still so relevant?

Bash has survived the bashing of time because of how it got intricately intertwined with Linux at its early age, not to mention that it is simply compelling. Linux gradually spread all over the world. We all know the story. Most of the web servers run on Linux. Android is based on Linux, and Linux is the backbone of IoT. As Linux spread around, so did Bash.

Especially talking about servers or IoT, most of the time, all developers get is a command-line interface to the system, not the beautiful GUIs that we are used to on Linux desktops. Even desktop users like to utilize the power of Bash. Most graphical applications developed for the Linux desktop use Bash commands to get their information. So as you can see, it is no surprise that Bash is so important for Linux.

What can you use Bash for?

Think of it like this: the desktop’s graphical interface is built on top of the command-line interface that existed before it. So unless it has something to do with graphics, like images or videos, you can do anything with Bash. Navigating your system; copying, moving, editing, or deleting files; managing system processes; managing applications installed on your system; connecting to remote systems; managing permissions and ownership; you name it, it probably exists.

How can you use Bash?

Using a Linux-based desktop all starts with opening up the terminal application. Ctrl+Alt+T should work on most systems, but search your application menu if it doesn’t.

Let’s start with some basics:

Listing Files

Enter this command in your terminal, and you should see the list of files in your home directory:

ls

Now let’s add some more details:

ls -la
Listing Files

Listing Files

The result of this command will show you the names of the files, but it will also show up hidden files in the directory, the permissions of the file, the owner of the file, the size, and some more information.

Moving to another directory

Moving to another directory is easy. For example, we are going to move to the Downloads directory here:

cd Downloads/

Now enter the listing command to see that you have moved to the Downloads directory.

Changing Directory

Changing Directory

Creating files

To create a file, all you need is the name and extension of that file. For example:

touch try.py
Creating a File

Creating a File

Here, I created a Python file with the name try. You can do that with any filename and any extension.

Deleting files

Deleting files can be done with the rm command:

rm try.py
Deleting a File

Deleting a File

Check the system monitor

There is a primary CLI-based system monitor that comes with Linux. It can be launched with this command:

top
The Top System Monitor

The Top System Monitor

Press Q to exit the system monitor.

For more information about these basic commands, check our article about 20 Linux commands that beginners should know.

Bash scripting

Sometimes, a situation calls for a whole script. For example, you have a situation where you need to enter a string of commands, one after another. And this situation presents itself very often. It is not efficient to enter these commands every single time, again and again. So instead, create a script in which you put in those commands in the order you want, and when you need to enter those commands, just executing that file would be enough.

In other words, Bash scripting is like a full-fledged programming language. You can create variables, functions, conditional statements, and everything you would do in any programming language like Python.

Diving deeper with Bash scripting

Bash isn’t just about running individual commands; it’s also a powerful scripting language. You can write scripts to automate repetitive tasks, making your life much easier. Here’s a basic example of a Bash script that creates a directory and then creates a file inside that directory:

#!/bin/bash

# Creating a directory named 'TestDir'
mkdir TestDir

# Changing directory to TestDir
cd TestDir

# Creating a file named 'TestFile.txt'
touch TestFile.txt

echo "Directory and file created successfully!"

Save this script as create_dir_and_file.sh, give it execute permissions using chmod +x create_dir_and_file.sh, and run it with ./create_dir_and_file.sh. You’ll see “Directory and file created successfully!” printed, and if you check, you’ll find a new directory TestDir with a file TestFile.txt inside.

FAQ: Bash shell in Linux

What is Bash shell used for?

Bash shell is primarily used as a command-line interface in Linux to interact with the operating system. It allows users to execute commands, run scripts, and manage files and directories. Bash is also used for scripting to automate tasks, making system administration more efficient.

Is Bash only for Linux?

While Bash is most commonly associated with Linux, it’s not exclusive to it. Bash is also available on other Unix-like operating systems, including macOS. Additionally, Windows users can access Bash through the Windows Subsystem for Linux (WSL) or third-party applications like Cygwin.

How do I start learning Bash scripting?

Starting with basic commands like pwd, ls, cd, and mkdir is a good foundation. From there, explore more advanced commands and their options. Practice writing simple scripts to automate tasks, such as file management or system updates. Online tutorials, forums, and books on Bash scripting are great resources for beginners.

Can Bash scripts be dangerous?

Yes, if not handled carefully. Bash scripts have the potential to execute powerful commands that can modify or delete critical system files. Always double-check your scripts before running them, especially if you’re using commands like rm (remove) or dd. Running scripts from untrusted sources is highly discouraged.

How do I run a Bash script?

To run a Bash script, you first need to make it executable. Use the command chmod +x scriptname.sh to grant execute permissions. Then, you can run the script by typing ./scriptname.sh in the terminal.

What’s the difference between Bash and the terminal?

The terminal, or terminal emulator, is the application used to access the command-line interface. Bash, on the other hand, is the command language run by the terminal. Think of the terminal as the book and Bash as the language the book is written in.

How can I find help for Bash commands?

Most Bash commands come with built-in help accessible through the man (manual) command, e.g., man ls, or the --help option, e.g., ls --help. These provide detailed information about the command’s usage and options. Additionally, online resources and forums are invaluable for troubleshooting and learning more complex aspects of Bash.

Is it necessary to learn Bash for Linux users?

While not strictly necessary, learning Bash greatly enhances your ability to interact with Linux efficiently. It’s particularly important for system administrators, developers, and those looking to understand the deeper workings of Linux systems.

Conclusion

Bash is one of the foundational pillars of the Linux ecosystem. Its usability, power, control, and speed have defied the test of time for more than 30 years now. If you want to use Linux as a simple desktop for basic tasks, you can get by without the knowledge of Bash, but if you’re going to become an advanced user and gain more power on the Linux front, Bash is an inescapable skill to learn. We hope this article was helpful. Cheers!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.