Automating Tasks with Shell Scripts

Sometimes, you’ll find yourself repeating the same few commands over and over, perhaps with slight variations. You reach the point of frustration, and say, “That’s it; I’m scripting this.” Being a CLI wizard, you do the following:

  1. Run tail -n 20 ~/.bash_history > myscript.sh to create a file that contains the last 20 Bash commands you ran.
  2. Then run bash myscript.sh to execute it.

Although this isn’t the recommended procedure (we’ll get to that in this chapter), it’s a perfectly valid way to create and run a Bash script.

This chapter is a Bash scripting crash course. Like any programming crash course, it is completely useless unless you actually follow along, type in all the code yourself, and run it in ...

Get The Software Developer's Guide to Linux 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.