What is Bash? – Opensource.com

When a computer boots, a kernel (whether Linux, BSD, Mach, or NT) recognizes all physical hardware and allows each component to communicate with each other and be orchestrated by some basic software. A computer’s most basic instruction set simply keeps it on and in a safe state: turn on fans periodically to prevent overheating, use subsystems to monitor disk space or “listen” for newly connected devices, and so on. If this were all computers did, they would be as interesting as a convection oven.

Computer scientists recognized this early on, so they developed a shell for Unix computers that operates outside the kernel (or around the kernel, like a shell in nature) and allows humans to interact with the computer whenever they want. It was an exciting development at a time when people were feeding punch cards into computers to tell them what to do. Of all the projectiles available, Bash is one of the most popular, most powerful and friendliest.

Bash is an application

When you launch a terminal (such as the GNOME or Konsole terminal on Linux or iTerm2 on macOS) that runs the Bash shell, you’re greeted with a message. A message is a symbol, usually a dollar sign ($), that indicates that the shell is waiting for your input. Of course, knowing what you’re supposed to write is another matter entirely.

This probably sounds hostile, but it’s actually a perfectly succinct representation of the many connotations around the term “Bash.” For many new users, there’s no separation between the concept of Bash and the concept of Linux or Unix: it’s the proverbial black screen with green text on which you’re supposed to code what your computer does next. That combines the Bash shell with the commands you type into the shell. It is important to understand that they are two separate things: Bash is just an application, and its main job is to run other applications (in the form of commands) that are installed on the same system.

[Download our free Bash cheat sheet

]

You can learn Bash, but only in the context of learning the operating system that\’s it runs on. Without knowing commands, there’s not much you can do with Bash.

Commands

Linux

On Linux and Unix (such as BSD and macOS), most commands are stored by default in system directories such as /usr/bin and /bin. By nature, Bash doesn’t know these commands any more than you naturally know Klingonese, but just as you can search for Klingon words, Bash can search for commands. When you issue a command to Bash, it searches for specific directories on your system to see if such a command exists. If the command exists, then Bash executes it.

Bash is also a command, and is usually the default command that runs when you open a terminal window or log in to a text console. To know where any command is located on your system, Bash included, you can use the which command in a terminal:

$ which bash /usr/bin/bash $ which ls /usr/bin/ls

Some commands are built into Bash. Most of the built-in commands are specific to Bash scripting or low-level environment configuration, but some are universally useful, such as cd (for changing directory). The built-in commands do not appear when you search for them because they do not exist in your usual executable path

: $ que bash which: no cd in (/usr/local/bin:/usr/bin:/bin:/bin: […]

They’are still available, however, because ‘they’re built into Bash, and Bash is what you’ is running.

Most

modern Linux and Unix distributions provide a Bash shell by default. They do this because Bash is well known, and it has several convenience features that other shells don’t have. However, some systems use another shell by default. To find out if you’re running a Bash shell, you can use the echo command along with a special variable that represents the name of the process that is currently running: $echo $

0 bash If you ‘

re not running Bash, but you’d want to try it out, you can probably download and install Bash from your software center, Software repository or port tree. Or you can use Chocolatey on Windows or Homebrew on macOS. If all else fails, visit the Bash homepage for more information.

Work in

Bash

Bash is a legitimate interface for your computer, and it’s not just for server administrators and programmers. It can be your desktop, your word processor, your graphics editing application, and much, much more. Some people use Bash more than they use desktop applications.

There are hundreds of commands available for Linux and Unix, and it may surprise you how diverse they are. For example, you can resize and crop photos without having to open the photo in a viewer or editor:

$ mogrify -geometry 1600^x800 \ -gravity Center \ -crop 1600×800+0+0 myphoto.jpg

You can play music with commands like ogg123 or mpg321, convert audio with sox, adjust and edit video with ffmpeg, edit text with emacs or vim, check email with pine or mutt, surf the web with elinks, browse files with Ranger or Midnightcommander, and do much, much more. Everything is done in Bash, using the commands found in your system or in your software repository.

Bash scripting

One reason Bash (and Linux in general) is considered so powerful is because it’s scriptable. Anything you can type into Bash manually, you can also list it in a plain text file and have Bash run it for you. Instead of spending an afternoon manually executing a hundred commands, you can program the commands and have your computer execute them while you take care of more important matters. Because almost everything on Linux runs on top of the Bash shell, almost everything on Linux can be programmed through Bash. While there are exceptions to this (graphical applications may have their own scripting language, for example, or no scripting at all), your operating system’s scripting opens up tens of thousands of possible functions that you can make happen on your computer without doing them yourself.

The amount of work this saves Linux users each day is impossible to estimate. However, it’s not the usual automation that makes the difference; It’s the workflows as people invent for themselves, the things no one else thinks need automation.

When experienced users say they want to learn Bash, if they don’t mean they want to learn Linux commands, then they probably mean they want to improve the way they create their commands. For example, this is an extremely rudimentary Bash script that converts a temporary file (imagine it’s a file created by a separate process) to a specific directory:

#!/usr/bin/bash cp tmp.png ~/public_html/’date +%Y%m%d’.png

That’s valid Bash. You can verify this by copying and pasting the command (the last line starting with cp) into a terminal. As long as there is a file named tmp.png and a directory named ~/public_html, the command works.

Learning Bash, however, is about understanding how a simple command like this can be improved for the sake of automation. For example, if the tmp.png file does not exist, then the script fails. If this script is a key component for, say, a blogging site that requires a new image every day so that a custom header image can be built, then the script failure could cause catastrophic errors elsewhere. A user who knows Bash could add resiliency using Bash syntax:

#!/usr/bin/bash IMG=”tmp.png” [[ -e tmp.png ]] || IMG=”generic.png” cp ~/”${IMG}” ~/public_html/’date +%Y%m%d’.png

This is just one example of the process of learning to write with Bash, but it demonstrates how learning both Linux and Bash are equally useful tasks and not completely separate.

Advantages of Bash

Bash

is as powerful as other shells, but adds convenience features such as double brackets ([[ and ]] ) in the sample code. These “Bashisms” are beloved by Bash users because they avoid the sometimes detailed and awkward syntax in other shells such as tcsh or ash. However, they are unique to Bash and are not POSIX compliant, which could cause compatibility issues on systems that do not run Bash. Moreover, Bash is free open source software, so most users can install it if they need to. The lack of support only forces an additional dependency and does not exclude anyone from using a script.

If you want to learn Bash, you can

read any number of great books on the subject

.

Or, you can take an online course from EdX.org and even join some free Red Hat courses

.

Best of all, you can also play games to learn Bash.

Good luck and happy hacking!