Chapter 2. Bash Primer

Bash is more than just a simple command-line interface for running programs. It is a programming language in its own right. Its default operation is to launch other programs. As we said earlier, when several words appear on the command line; bash assumes that the first word is the name of the program to launch and the remaining words are the arguments to pass to that program.

But as a programming language, it also has features to support input and output, and control structures such as if, while, for, case, and more. Its basic data type is strings (such as filenames and pathnames) but it also supports integers. Because its focus is on scripts and launching programs and not on numerical computation, it doesn’t directly support floating-point numbers, though other commands can be used for that. Here, then, is a brief look at some of the features that make bash a powerful programming language, especially for scripting.

Output

As with any programming language, bash has the ability to output information to the screen. Output can be achieved by using the echo command:

$ echo "Hello World"

Hello World

You may also use the printf built-in command, which allows for additional formatting:

$ printf "Hello World\n"

Hello World

You have already seen (in the previous chapter) how to redirect that output to files or to stderr or, via a pipe, into another command. You will see much more of these commands and their options in the pages ahead.

Variables

Bash variables ...

Get Cybersecurity Ops with bash now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.