Bash Quick Start Guide
eBook - ePub

Bash Quick Start Guide

Get up and running with shell scripting with Bash

  1. 186 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Bash Quick Start Guide

Get up and running with shell scripting with Bash

About this book

Learn how to write shell script effectively with Bash, to quickly and easily write powerful scripts to manage processes, automate tasks, and to redirect and filter program input and output in useful and novel ways.

Key Features

  • Demystify the Bash command line
  • Write shell scripts safely and effectively
  • Speed up and automate your daily work

Book Description

Bash and shell script programming is central to using Linux, but it has many peculiar properties that are hard to understand and unfamiliar to many programmers, with a lot of misleading and even risky information online. Bash Quick Start Guide tackles these problems head on, and shows you the best practices of shell script programming.

This book teaches effective shell script programming with Bash, and is ideal for people who may have used its command line but never really learned it in depth. This book will show you how even simple programming constructs in the shell can speed up and automate any kind of daily command-line work.

For people who need to use the command line regularly in their daily work, this book provides practical advice for using the command-line shell beyond merely typing or copy-pasting commands into the shell. Readers will learn techniques suitable for automating processes and controlling processes, on both servers and workstations, whether for single command lines or long and complex scripts. The book even includes information on configuring your own shell environment to suit your workflow, and provides a running start for interpreting Bash scripts written by others.

What you will learn

  • Understand where the Bash shell fits in the system administration and programming worlds
  • Use the interactive Bash command line effectively
  • Get to grips with the structure of a Bash command line
  • Master pattern-matching and transforming text with Bash
  • Filter and redirect program input and output
  • Write shell scripts safely and effectively

Who this book is for

People who use the command line on Unix and Linux servers already, but don't write primarily in Bash. This book is ideal for people who've been using a scripting language such as Python, JavaScript or PHP, and would like to understand and use Bash more effectively.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Essential Commands

In this chapter, we'll be going through some of the commands essential for good Bash scripting. Bash programming, and shell script programming in general, is different from most other programming languages because it is designed to run other programs, mixing the commands it provides with commands installed on the local system.
It may seem strange that in a Bash book, we would spend half a chapter explaining how to use commands that are neither part of Bash, nor written in the Bash language! The reason we do this is that most useful Bash scripts will use several of these external commands to do their work, particularly to do things that in the Bash language are difficult, awkward, or even impossible.

Distinguishing command types

The commands you can use in a Bash script fall into three major categories:
  • Shell builtin commands: Included in Bash itself. These commands don't correspond to executable program files on your system; they are implemented in the bash binary itself. Examples are echo, type, and source.
  • Runtime commands: Defined in the shell at runtime, and written in the Bash language. These can be aliases or functions. They don't have executable program files of their own on disk either, and are defined at runtime during a Bash session, often by reading startup files. Examples vary between systems and users.
  • System commands: Invoke executable program files on your filesystem. These are the only kinds of commands that can also be run outside of Bash. Examples are grep, ping, and rm.
The executable programs called by system commands may be written in any language. Bash can be used to specify how these programs run, where their input comes from, how their output is processed, and how their success or failure changes the execution of the rest of the script.
We will explore useful commands in the first and third categories in this chapter. Runtime commands (aliases and functions) are discussed in Chapter 7, Scripts, Functions, and Aliases. In this chapter, you will learn how to use these essential builtin commands:
  • type: Finding what a command is
  • echo: Printing arguments
  • printf: Printing formatted arguments
  • pwd: Printing the current directory
  • cd: Changing the current directory
  • set: Viewing and setting shell properties
  • declare: Managing variables and functions
  • test, [, [[: Evaluating expressions
You will also learn how to use these system commands, which are not part of Bash itself:
  • ls: Listing files for users
  • mv: Moving and renaming files
  • cp: Copying files
  • rm and rmdir: Deleting files and directories
  • grep: Matching patterns
  • cut: Extracting columns from data
  • wc: Counting lines, words, and characters
  • find: Iterating through a file tree
  • sort and uniq: Sorting and de-duplicating input
We will not be covering the use of interactive text editors, such as emacs, nano, and vi, due to space concerns. It's a very good idea for an aspiring Bash programmer to learn how to use one of those editors, however! Your author recommends vi, especially the Vim implementation.

Essential Bash builtin commands

The first category of essential commands that we'll examine are builtins: they are included as part of the bash program. In fact, the very first command we'll look at, called type, is itself designed to help you tell what any kind of command is.
For all of these commands, Bash can provide help on usage with the help program. For example, to get help on the type command, you would type:
bash$ help type

The type command

The type command, given the name of any command or commands, gives you information about what kind of command it is:
bash$ type echo echo is a shell builtin bash$ type grep grep is /bin/grep
It identifies shell keyword, too:
bash$ type for for is a shell keyword
We can define a function and an alias to test that it correctly detects those:
bash$ myfunc() { : ; } bash$ type myfunc myfunc is a function
myfunc ()
{
:
}
bash$ alias myalias=: bash$ type myalias myalias is aliased to `:'
The type command has a few useful options. If you use the -t option, you can get a single word specifying the command type. This is sometimes useful in scripts:
bash$ type -t echo builtin bash$ type -t grep file
If you use the -a option, you can see all commands that have the same name. Bash will print them in order of preference. For example, the true name probably has both a builtin command and a system command on your system:
bash$ type -a true true is a shell builtin true is /bin/true
Bash will always prefer builtin commands for a given name over system commands. If you want to call the system's implementation of true, you could do it by specifying the full path to the program:
$ /bin/true
Try type -a [ and then type -a [[. Are you surprised by any of the output?
Another useful switch for type is -P, which will look for a system command for the given name, and print out the path to it if found:
bash$ type -P true /bin/true
Note that the /bin/true path was returned, even though true is also the name of a shell builtin.
There is a reason we put the type command first in this list. After help, it is the most useful Bash command for understanding the language, and clearing up the biggest source of confusion about the language: When I run this command, what is actually running?

The echo command

The echo builtin command just repeats the arguments you provide it onto the standard output:
$ echo Hello Hello
This makes it a simple way to emit content to the terminal, including variables:
$ echo 'Hello, '"$USER"\! Hello, bashuser!
The echo command in Bash has switches that allow you to control the output, such as excluding the final newline, or expanding sequences such as \t to tab characters. However, we caution against using echo in scripts, because for historical reasons it has broken design and portability problems that makes it confusing or error-prone to use, especially when trying to use it with switches. We suggest you always use printf in scripts instead, as its behavior is more predictable, especially when printing variables or control characters such as newlines.

The printf command

The printf command works like echo, except the first argument you provide to it is a format string:
$ printf '%s\n' 'Hello!' Hello!
Notice that we had to put the format string in single quotes, to prevent the backslash from having special meaning to the shell. Notice also that we didn't have to use double quotes to get the newline from \n; the printf command did that for us.
You can use printf in much the same way you would use a printf() function in other languages, such as C. It supports most of the same format specifications as the printf(1) system command; you can type man 1 printf to see a list.
It's easier to print tricky strings with printf, where echo might struggle:
$ printf '%s\n' -n -n $ string=-n $ printf '%s\n' "$string" -n
You should always choose printf over echo in scripts, even though it's a little more typing.
Be careful with what you put into a format string; a variable with a value provided by the user can be a security hole! It's best to use only fixed format strings, or carefully build them yourself.
printf also has a useful property where it will repeat the format string as necessary for each argument you provide it. This is a convenient way to print a list of arguments on individual lines:
$ printf '%s\n' foo bar baz foo bar baz
Note that we got three instances of the string-newline pattern from one format string.
Finally, Bash's printf has a %q pattern that can be used to quote special characters in a string with backslashes, so it can be reused safely in the shell. If you follow good quoting practices, you are unlikely to need this a lot in Bash, but it's useful to know it's there:
bash$ printf '%q\n' 'Watch out for thi$ $tring; it \has\ nasty character$!' Watch\ out\ for\ thi\$\ \$tring\;\ it\ \\has\\\ nasty\ character\$\!

The pwd command

The pwd Bash builtin prints the current working directory f...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Contributors
  6. Preface
  7. What is Bash?
  8. Bash Command Structure
  9. Essential Commands
  10. Input, Output, and Redirection
  11. Variables and Patterns
  12. Loops and Conditionals
  13. Scripts, Functions, and Aliases
  14. Best Practices
  15. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Bash Quick Start Guide by Tom Ryder in PDF and/or ePUB format, as well as other popular books in Computer Science & System Administration. We have over one million books available in our catalogue for you to explore.