A Bit of Bash
Bash is an indispensable tool for software developers and system administrators of all kinds, but it isn’t just for developers and administrators. Countless designers, artists, project managers, data scientists, and more use it every day to accomplish a wide variety of tasks and to save time.
If you do not already use Bash then you are in for a treat. In this first of what we hope will be a series of articles we will go over what Bash is and some common things for which people use it so you can get some idea of what it can do for you. If you have used Bash before, we hope you enjoy this refresher and share it with your friends and colleagues. Either way, please share your thoughts and experiences in the comments and let us know what you would like to see in future articles.
What is Bash?
In short, Bash is a shell. That is, it is a command-line interpreter for computers—a way for users to interact with computers by typing text commands. When you start an application like Terminal on macOS (formerly OS X) or Ubuntu, you are running a shell and it is probably Bash.
There are other shells—the name “Bash” actually comes from “Bourne Again Shell”, referencing a previously popular shell—but Bash is probably the most popular. It is the default shell in macOS and popular Linux distributions such as Ubuntu and it is even available in Windows since the Windows 10 Anniversary Update.
Bash allows you to run commands one at a time, in groups, and even within scripts. It is often used to access other programs, including both command-line and GUI programs.
Why Use a Command Line? Speed, Convenience, and Flexibility
Command lines were around long before icons, windows, and mouse pointers, but these days most people never use them, thinking of them as obsolete or old fashioned or not thinking of them at all. They have always been around, but they are often hidden away because it is usually easier to accomplish tasks wish clicks and drags. Usually, but not always.
As it happens, some tasks can be quite fast and convenient via a command line, even when they can also be accomplished via mouse or touchscreen. For example, most modern software developers use source control systems such as Git and while graphical programs such as SourceTree and Tower provide elegant interfaces and useful visualizations it is often preferable to perform quick operations or view logs via command line. Especially if one wants to use options that are not exposed via a graphical interface, filter or process output via another program, or save or send such customized, filtered, and processed data as reports.
A command line is sometimes the only way to interact with a computer. Linux computers, especially servers, can be used without windowing systems even being installed and even if windowing systems are installed, such computers may be run without displays or they may be in other physical locations. And even if a computer has a fully featured windowing system with a display or a remote desktop service and it is accessible physically, access may be limited to certain users or groups or groups of users. Shared web hosting services often grant terminal access—that is, command-line access—and file-transfer access (e.g., FTP and SFTP) without graphical access.
Form for Commands
Shape of… text! Form of… more text!
The simplest Bash commands are just one word long—the name of a system command or other program that can do something for you without you having to specify any addition details. A system command or other program is not necessarily specific to Bash, but if you are likely to run it via Bash then that is enough for our present purpose.
In this case, a “word” is really three words compounded into one:
“whoami” is a system command that prints the current username. An equivalent command shows that commands can also be used with additional information, which we call arguments or flags:
In “id -un” the “-un” part is an argument that is passed to the id command. In this case, the argument is comprised of a hyphen followed by the “u” and “n” flags. Don’t worry if it takes a while for that to sink in. With a little practice and repetition, it can become familiar.
Commands can receive multiple arguments, which can take multiple forms. They can be simple or complex, but for now we’ll keep things simple. Let’s look at some practical examples.
Common Tasks: Checking Program Versions and Installation Locations
Imagine that a team member has prepared a Python script to automate a common task. Very helpful! But your associate wants to know which version of Python you have installed. How do you check?
As it happens, if you invoke the “python” command with the “V” flag it will tell you its version.
Most command-line programs will reveal their versions in response to some kind of argument, although the argument itself may differ. Here are some more common examples:
node -V npm -V httpd -V mysql -V php -V perl -V grunt -V gulp -V
Knowing the version of a program is good, but what if you have multiple versions installed and you aren’t sure which one is run when you type its command? What if you need to know a command’s explicit installation location for some script or configuration file? The “which” command can tell you:
The version of a program and the path to its default version can often come in handy when getting a script to run. You may need to provide that information to a colleague or use it to configure your system path.
Common Tasks: Getting More Information About a Program
Further information about most system commands and other robust programs is available via the “man” command. For example:
man bash man python man node
Common Tasks: General Software Development
A lot of developers like to use source control systems such as Git and Subversion from the command line. Here are a few examples of common Git commands:
git status git pull git push git commit -m "do the thing" git commit --amend -m "do the thing"
Common Tasks: Front-End Web Development
grunt grunt dev grunt test grunt build
Whether or not you are technically savvy, Bash can help you improve the efficiency and productivity of your workflow. It can help you save time or even just customize your workflow to your personal preferences. And this overview has barely scratched the surface of what Bash can do.