Web server setup - CHAPTER 17 COOL SHELL TRICKS While it

CHAPTER 17 COOL SHELL TRICKS While it might not seem very useful for day-to-day work, Ctrl+D is vital for programs that expect you to enter data at the command line. You might run into these as you explore BASH. If ever you read in a man page that a program requires an EOF message during input, you ll know what to press. Using the Command History The original hackers who invented the tools used under Unix hated waiting around for things to happen. After all, being a hacker is all about finding the most efficient way of doing any particular task. Because of this, the BASH shell includes many features designed to optimize the user experience. The most important of these is the command history. BASH remembers every command you enter (even the ones that don t work!) and stores them as a list on your hard disk. During any BASH session, you can cycle through this history using the up and down arrow keys. Pressing the up arrow key takes you back into the command history, and pressing the down arrow key takes you forward. The potential of the command history is enormous. For example, rather than retype that long command that runs a program with command options, you can simply use the cursor keys to locate it in the history and press Enter. Tip Typing !-3 will cause BASH to move three paces back in the history file and run that command. In other words, it will run what you entered three commands ago. On my Ubuntu test system, BASH remembers 1000 commands. You can view all of the remembered commands by typing history at the command prompt. The history list will scroll off the screen because it s so large, but you can use the scroll bars of the GNOME Terminal window to read it. Each command in the history list is assigned a number. You can run any of the history commands by preceding their number with an exclamation mark (!), referred to as a bang, or sometimes a shriek. For example, you might type !923. On my test system, command number 923 in the BASH history is cd .., so this has the effect of switching me into the parent directory. Command numbering remains in place until you log out (close the GNOME Terminal window or end a virtual console session). After this, the numbering is reordered. There will still be 1000 commands, but the last command you entered before logging out will be at the end of the list, and the numbering will work back 1000 places until the first command in the history list. Tip One neat trick is to type two bangs: !!. This tells BASH to repeat the last command you entered. Rather than specifying a command number, you can type something like !cd. This will cause BASH to look in the history file, find the last instance of a command line that started with cd, and then run it.

Leave a Reply