Learning Shell Scripting with Zsh
Format: PDF / Kindle (mobi) / ePub
Your onestop guide to reading, writing, and debugging simple and complex Z shell scripts
About This Book
- A step-by-step guide that will show you how to use zsh and its repertoire of powerful features to improve the efficiency of your daily tasks
- Learn how to configure and use zsh
- Discover some advanced features of zsh such as process and parameter substitution, running on restricted functionality mode, and emulating other shells
Who This Book Is For
If you are a system administrator, developer, or computer professional involved with UNIX who are looking to improve on their daily tasks involving the UNIX shell, "Learning Shell Scripting with zsh" will be great for you. It's assumed that you have some familiarity with an UNIX command-line interface and feel comfortable with editors such as Emacs or vi.
What You Will Learn
- Set up and configure the advanced aspects of zsh such as process and parameter substitution, running on restricted functionality mode, and emulating other shells, for example, ksh and Bourne
- Customize the shell prompt to display the information you need
- Work with the line-editor module to type less and do more
- Use filename generation, also known as globbing, to work with files and folders
- Discover programmable completion and never mistype a command or its options again
- Tame the shell history and use it simultaneously on multiple shells with just a few keystrokes
- Expand on zsh's built-in modules and functionality by creating your own functions
Zsh has become incredibly popular in recent times and reasonably so; the shell boasts some really great features such as command completion, filename generation, and history sharing among instances of the shell. Learning to use them to your advantage will prove to be really valuable and will save you from lots of tedious and overly-complex tasks.
This hands-on guide will show you how to configure and use zsh for work and daily tasks. It provides a clear introduction to the more powerful features of the shell such as globbing and completion. This book will help you take advantage of the real power behind the UNIX shell on both desktops and servers through real-world examples.
This book will help you understand the features of zsh and make it one of the most valuable assets in your toolbox. It will teach you how to use the shell history to look up and edit previously typed entries and take advantage of the line-editor module to speed up your typing and editing of commands. You will be able to create your own completion functions and ensure you never mistype a command or have to look at the manpages again.
You will learn how to define your own aliases and learn about filename generation so you can do more by typing even less. You will also discover the essentials of zsh and go deeper with its more advanced features. When you are done with this book, your time with the command line will be much more enjoyable and productive.
things: % more !1573$ % more zsh/zsh_funcs As you can see, we used the bang operator together with the $ selector to refer to the last argument of line 1573 of our history. Interestingly, you can also use a negative integer to refer to the nth-to-last entry: % !-2 # this will retrieve the 2nd to last entry in history. % !-97 # this does the same to the 97th to last entry. Negative indexes should be pretty familiar territory for some programmers (I'm looking at you, Python and Ruby developers).
Chapter 3 Having a default set in your startup files does not mean you have to commit to it at all times though. You can switch between vi and Emacs modes respectively, simply by typing the following line: % bindkey -e or % bindkey -v By using the e or v options, you are telling bindkey to link the provided emacs or viins keymaps to the main alias, which in turn gets loaded by default during startup. If anything goes awry, ZLE will default to .safe, which is a very constrained mode that
Let's try that again, but now we'll set the following option: % setopt no_bad_pattern % echo *[[:alpha:] *[[:alpha:] We turned on NO_BAD_PATTERN (or unset BAD_PATTERN, whatever floats your boat) and guess what happened? That's right; the bad pattern is ignored by the shell expansion mechanism and passed instead as an argument to the command. Pretty handy if you don't want those pesky warnings while experimenting with your newly learned patterns. Extended Globbing As you might have noticed at
log_002.txt log_010.txt log_001.txt log_009.txt log_030.txt log_031.txt We want to work with those files that match the log_xxx.txt pattern, where xxx is a digit. Let's put what we just learned to good use: % echo log_<->.txt log_001.txt log_002.txt log_009.txt log_010.txt log_030.txt log_031.txt What if we want those logfiles from 10 upwards? Zsh has you covered: % echo log_<10->.txt log_010.txt log_030.txt log_031.txt As you can see, the <-> pattern can define a range with lower and
function on startup, making it available to your session. You can now just type zmv and you'll be greeted with a fairly straightforward set of instructions. Basically, the zmv syntax expects two patterns: one for matching filenames and a second one into which the results will be converted: zmv [OPTIONS] old_pattern new_pattern As you might have guessed, zmv goes along with a great deal of Globbing, which is why we are only getting acquainted with it now. Here's how we can use it to rename our