Logo Passei Direto
Buscar
Material
páginas com resultados encontrados.
páginas com resultados encontrados.
left-side-bubbles-backgroundright-side-bubbles-background

Crie sua conta grátis para liberar esse material. 🤩

Já tem uma conta?

Ao continuar, você aceita os Termos de Uso e Política de Privacidade

left-side-bubbles-backgroundright-side-bubbles-background

Crie sua conta grátis para liberar esse material. 🤩

Já tem uma conta?

Ao continuar, você aceita os Termos de Uso e Política de Privacidade

left-side-bubbles-backgroundright-side-bubbles-background

Crie sua conta grátis para liberar esse material. 🤩

Já tem uma conta?

Ao continuar, você aceita os Termos de Uso e Política de Privacidade

left-side-bubbles-backgroundright-side-bubbles-background

Crie sua conta grátis para liberar esse material. 🤩

Já tem uma conta?

Ao continuar, você aceita os Termos de Uso e Política de Privacidade

left-side-bubbles-backgroundright-side-bubbles-background

Crie sua conta grátis para liberar esse material. 🤩

Já tem uma conta?

Ao continuar, você aceita os Termos de Uso e Política de Privacidade

left-side-bubbles-backgroundright-side-bubbles-background

Crie sua conta grátis para liberar esse material. 🤩

Já tem uma conta?

Ao continuar, você aceita os Termos de Uso e Política de Privacidade

left-side-bubbles-backgroundright-side-bubbles-background

Crie sua conta grátis para liberar esse material. 🤩

Já tem uma conta?

Ao continuar, você aceita os Termos de Uso e Política de Privacidade

left-side-bubbles-backgroundright-side-bubbles-background

Crie sua conta grátis para liberar esse material. 🤩

Já tem uma conta?

Ao continuar, você aceita os Termos de Uso e Política de Privacidade

Prévia do material em texto

<p>The Definitive Guide to Bash Command Line History</p><p>catonmat.net/the-definitive-guide-to-bash-command-line-history</p><p>Last updated 3 weeks ago</p><p>Let me teach you how to work efficiently with command line</p><p>history in bash.</p><p>This tutorial comes with a downloadable cheat sheet that</p><p>summarizes (and expands on) topics covered in this guide.</p><p>In case you are a first time reader, this is the 3rd part of the</p><p>article series on working efficiently in bourne again shell.</p><p>Previously I have written on how to work efficiently in vi and</p><p>emacs command editing modes by using predefined keyboard shortcuts (both articles</p><p>come with cheat sheets of predefined shortcuts).</p><p>First, lets review some basic keyboard shortcuts for navigating around previously typed</p><p>commands.</p><p>As you remember, bash offers two modes for command editing - emacs mode and vi</p><p>mode. In each of these editing modes the shortcuts for retrieving history are different.</p><p>Suppose you had executed the following commands:</p><p>$ echo foo bar baz</p><p>$ iptables -L -n -v -t nat</p><p>$ ... lots and lots more commands</p><p>$ echo foo foo foo</p><p>$ perl -wle 'print q/hello world/'</p><p>$ awk -F: '{print$1}' /etc/passwd</p><p>$</p><p>and you wanted to execute the last command (awk -F ...).</p><p>You could certainly hit the up arrow and live happily along, but do you really want to</p><p>move your hand that far away?</p><p>If you are in emacs mode just try CTRL-p which fetches the previous command from</p><p>history list (CTRL-n for the next command).</p><p>In vi mode try CTRL-[ (or ESC) (to switch to command mode) and 'h' ('j' for the next</p><p>command).</p><p>There is another, equally quick, way to do that by using bash's history expansion</p><p>mechanism - event designators. Typing '!!' will execute the previous command (more</p><p>about event designators later).</p><p>1/8</p><p>https://catonmat.net/the-definitive-guide-to-bash-command-line-history</p><p>https://catonmat.net/bash-vi-editing-mode-cheat-sheet</p><p>https://catonmat.net/bash-emacs-editing-mode-cheat-sheet</p><p>Now, suppose that you wanted to execute 'iptables -L -n -v -t nat ' command again</p><p>without retyping it.</p><p>A naive user would, again, just keep hitting up-arrow key until he/she finds the</p><p>command. But that's not the way hackers work. Hackers love to work quickly and</p><p>efficiently. Forget about arrow keys and page-up, page-down, home and end keys. They</p><p>are completely useless and, as I said, they are too far off from the main part of the</p><p>keyboard anyway.</p><p>In emacs mode try CTRL-r and type a few first letters of 'iptables', like 'ipt'. That will</p><p>display the last iptables command you executed. In case you had more than one iptables</p><p>commands executed in between, hitting CTRL-r again will display older entries. In case</p><p>you miss the right command and move too deep into history list, you can reverse the</p><p>search direction by hitting CTRL-s (don't forget that by default CTRL-s stops the output to</p><p>the terminal and you'll get an effect of "frozen" terminal (hit CTRL-q to "unfreeze"), see</p><p>stty command to change this behavior).</p><p>In vi mode the same CTRL-r and CTRL-s still work but there is another way more specific</p><p>to vi mode.</p><p>Switch to command mode by hitting CTRL-[ or ESC and hit '/', then type a first few</p><p>characters of 'iptables' command, like 'ipt' and hit return. Bash will display the most</p><p>recent match found in history. To navigate around use 'n' or just plain '/' to repeat the</p><p>search in the same direction, and 'N' or '?' to repeat the search in opposite direction!</p><p>With event designators you may execute only the most recently executed command</p><p>matching (or starting with) 'string'.</p><p>Try '!iptables' history expansion command which refers to the most recent command</p><p>starting with 'iptables'.</p><p>Another way is to use bash's built in 'history' command then grep for a string of interest</p><p>and finally use an event designator in form '!N', where N is an integer which refers to N-</p><p>th command in command history list.</p><p>For example,</p><p>$ history | grep 'ipt'</p><p>2 iptables -L -n -v -t nat</p><p>$ !2 # will execute the iptables command</p><p>I remembered another way to execute N-th command in history list in vi editing mode.</p><p>Type 'N' (command number) and then 'G', in this example '2G'</p><p>Listing and Erasing Command History</p><p>Bash provides a built-in command 'history' for viewing and erasing command history.</p><p>Suppose that we are still working with the same example:</p><p>2/8</p><p>$ echo foo bar baz</p><p>$ iptables -L -n -v -t nat</p><p>$ ... lots and lots more commands</p><p>$ echo foo foo foo</p><p>$ perl -wle 'print q/hello world/'</p><p>$ awk -F: '{print$1}' /etc/passwd</p><p>$</p><p>Typing 'history' will display all the commands in bash history alongside with line</p><p>numbers:</p><p>1 echo foo bar baz</p><p>2 iptables -L -n -v -t nat</p><p>... lots and lots more commands</p><p>568 echo foo foo foo</p><p>569 perl -wle 'print q/hello world/'</p><p>570 awk -F: '{print$1}' /etc/passwd</p><p>Typing 'history N', where N is an integer, will display the last N commands in the history.</p><p>For example, 'history 3' will display:</p><p>568 echo foo foo foo</p><p>569 perl -wle 'print q/hello world/'</p><p>570 awk -F: '{print$1}' /etc/passwd</p><p>history -c will clear the history list and history -d N will delete a history entry N.</p><p>By default, the history list is kept in user's home directory in a file ' .bash_history'.</p><p>History Expansion</p><p>History expansion is done via so-called event designators and word designators.</p><p>Event designators can be used to recall previously executed commands (events) and</p><p>word designators can be used to extract command line arguments from the events.</p><p>Optionally, various modifiers can be applied to the extracted arguments.</p><p>Event designators are special commands that begin with a ' !' (there is also one that</p><p>begins with a '^'), they may follow a word designator and one or more modifiers. Event</p><p>designators, word designators and modifiers are separated by a colon ':'.</p><p>Event Designators</p><p>Lets look at a couple of examples to see how the event designators work.</p><p>Event designator '!!' can be used to refer to the previous command, for example,</p><p>$ echo foo bar baz</p><p>foo bar baz</p><p>$ !!</p><p>foo bar baz</p><p>3/8</p><p>Here the '!!' executed the previous 'echo foo bar baz' command.</p><p>Event designator '!N' can be used to refer to the N-th command.</p><p>Suppose you listed the history and got the following output:</p><p>1 echo foo foo foo</p><p>2 iptables -L -n -v -t nat</p><p>... lots and lots more commands</p><p>568 echo bar bar bar</p><p>569 perl -wle 'print q/hello world/'</p><p>570 awk -F: '{print$1}' /etc/passwd</p><p>Then the event designator '!569' will execute 'perl ...' command, and '!1' will execute</p><p>'echo foo foo foo' command!</p><p>Event designator '!-N' refers to current command line minus N . For example,</p><p>$ echo foo bar baz</p><p>foo bar baz</p><p>$ echo a b c d e</p><p>a b c d e</p><p>$ !-2</p><p>foo bar baz</p><p>Here the event designator '!-2' executed a one before the previous command, or current</p><p>command line minus 2.</p><p>Event designator '!string' refers to the most recent command starting with 'string'. For</p><p>example,</p><p>$ awk --help</p><p>$ perl --help</p><p>Then the event designator '!p' or '!perl' or '!per' will execute the 'perl --help ' command.</p><p>Similarly, '!a' will execute the awk command.</p><p>An event designator '!?string?' refers to a command line containing (not necessarily</p><p>starting with) 'string'.</p><p>Perhaps the most interesting event designator is the one in form '^string1^string2^'</p><p>which takes the last command, replaces string1 with string2 and executes it. For</p><p>example,</p><p>$ ehco foo bar baz</p><p>bash: ehco: command not found</p><p>$ ^ehco^echo^</p><p>foo bar baz</p><p>Here the '^ehco^echo^' designator replaced the incorrectly typed 'ehco' command with</p><p>the correct 'echo' command and executed it.</p><p>4/8</p><p>Word Designators and Modifiers</p><p>Word designators follow event designators separated by a colon. They are used to refer</p><p>to some or all of the parameters on the command referenced by event designator.</p><p>For example,</p><p>$ echo a b c d e</p><p>a b c d e</p><p>$ echo !!:2</p><p>b</p><p>This is the simplest form of a word designator. ':2' refers to the 2nd argument of the</p><p>command (3rd word). In general ':N' refers to Nth argument of the command ((N+1)-th</p><p>word).</p><p>Word designators also accept ranges, for example,</p><p>$ echo a b c d e</p><p>a b c d e</p><p>$ echo !!:3-4</p><p>c d</p><p>There are various shortcuts, such as, ':$' to refer to the last argument, ':^' to refer to the</p><p>first argument, ':*' to refer to all the arguments (synonym to ':1-$'), and others. See the</p><p>cheat sheet for a complete list.</p><p>Modifiers can be used to modify the behavior of a word designators. For example:</p><p>$ tar -xvzf software-1.0.tgz</p><p>software-1.0/file</p><p>...</p><p>$ cd !!:$:r</p><p>software-1.0$</p><p>Here the 'r' modifier was applied to a word designator which picked the last argument</p><p>from the previous command line. The 'r' modifier removed the trailing suffix '.tgz'.</p><p>The 'h' modifier removes the trailing pathname component, leaving the head:</p><p>$ echo /usr/local/apache</p><p>/usr/local/apache</p><p>$ echo !!:$:h</p><p>/usr/local</p><p>The 'e' modifier removes all but the trailing suffix:</p><p>$ ls -la /usr/src/software-4.2.messy-Extension</p><p>...</p><p>$ echo /usr/src/*!!:$:e</p><p>/usr/src/*.messy-Extension # ls could have been used instead of echo</p><p>5/8</p><p>Another interesting modifier is the substitute ':s/old/new/' modifier which substitutes</p><p>new for old. It can be used in conjunction with 'g' modifier to do global substitution. For</p><p>example,</p><p>$ ls /urs/local/software-4.2 /urs/local/software-4.3</p><p>/usr/bin/ls: /urs/local/software-4.2: No such file or directory</p><p>/usr/bin/ls: /urs/local/software-4.3: No such file or directory</p><p>$ !!:gs/urs/usr/</p><p>...</p><p>This example replaces all occurances of 'urs' to 'usr' and makes the command correct.</p><p>There are a few other modifiers, such as 'p' modifier which prints the resulting command</p><p>after history expansion but does not execute it. See the cheat sheet for all of the</p><p>modifiers.</p><p>Modifying History Behavior</p><p>Bash allows you to modify which commands get stored in the history list, the file where</p><p>they get stored, the number of commands that get stored, and a few other options.</p><p>These options are controlled by setting HISTFILE, HISTFILESIZE, HISTIGNORE and</p><p>HISTSIZE environment variables.</p><p>HISTFILE, as the name suggests, controls where the history file gets saved.</p><p>For example,</p><p>$ export HISTFILE=/home/pkrumins/todays_history</p><p>will save the commands to a file /home/pkrumins/todays_history</p><p>Set it to /dev/null or unset it to avoid getting your history list saved.</p><p>HISTFILESIZE controls how many history commands to keep in HISTFILE.</p><p>For example,</p><p>$ export HISTFILESIZE=1000</p><p>will keep the last 1000 history commands.</p><p>HISTSIZE controls how many history commands to keep in the history list of current</p><p>session.</p><p>For example,</p><p>$ export HISTSIZE=42</p><p>will keep 42 last commands in the history of current session.</p><p>If this number is less than HISTFILESIZE, only that many commands will get written to</p><p>HISTFILE.</p><p>6/8</p><p>HISTIGNORE controls the items which get ignored and do not get saved. This variable</p><p>takes a list of colon separated patterns. Pattern '&' (ampersand) is special in a sense that</p><p>it matches the previous history command.</p><p>There is a trick to make history ignore the commands which begin with a space. The</p><p>pattern for that is "[ ]*"</p><p>For example,</p><p>$ export HISTIGNORE="&:[ ]*:exit"</p><p>will make bash ignore duplicate commands, commands that begin with a space, and the</p><p>'exit' command.</p><p>There are several other options of interest controlled by the built-in 'shopt' command.</p><p>The options may be set by specifying ' -s' parameter to the 'shopt' command, and may be</p><p>unset by specifying '-u' parameter.</p><p>Option 'histappend' controls how the history list gets written to HISTFILE, setting the</p><p>option will append history list of current session to HISTFILE, unsetting it (default) will</p><p>make HISTFILE get overwritten each time.</p><p>For example, to set this option, type:</p><p>$ shopt -s histappend</p><p>And to unset it, type:</p><p>$ shopt -u histappend</p><p>Option 'histreedit' allows users to re-edit a failed history substitution.</p><p>For example, suppose you had typed:</p><p>$ echo foo bar baz</p><p>and wanted to substitute 'baz' for 'test' with the ^baz^test^ event designator , but you</p><p>made a mistake and typed ^boo^test^. This would lead to a substitution failure because</p><p>the previous command does not contain string 'boo'.</p><p>If you had this option turned on, bash would put the erroneous ^baz^test^ event</p><p>designator back on the command line as if you had typed it again.</p><p>Finally, option 'histverify' allows users to verify a substituted history expansion.</p><p>Based on the previous example, suppose you wanted to execute that 'echo' command</p><p>again by using the '!!' event designator. If you had this option on, bash would not execute</p><p>the 'echo' command immediately but would first put it on command line so that you</p><p>could see if it had made the correct substitution.</p><p>7/8</p><p>Tuning the Command Prompt</p><p>Here is how my command prompt looks:</p><p>Wed Jan 30@07:07:03</p><p>pkrumins@catonmat:1002:2:~$</p><p>The first line displays the date and time the command prompt was displayed so I could</p><p>keep track of commands back in time.</p><p>The second line displays username, hostname, global history number and current</p><p>command number.</p><p>The global history number allows me to quickly use event designators.</p><p>My PS1, primary prompt display variable looks like this:</p><p>PS1='\d@\t\n\u@\h:\!:\#:\w$ '</p><p>Bash History Cheat Sheet</p><p>For your convenience, here's the bash history cheat sheet one more time. This cheat</p><p>sheet includes:</p><p>History editing keyboard shortcuts (emacs and vi mode)</p><p>History expansion summary - event designators, word designators and modifiers</p><p>Shell variables and `shopt' options to modify history behavior</p><p>Examples</p><p>It's available in three formats:</p><p>See you next time!</p><p>Thanks for reading my post. If you enjoyed it and would like to receive my posts</p><p>automatically, you can subscribe to new posts via rss feed or email.</p><p>Working Productively in Bash's Vi Command Line Editing Mode (with Cheat Sheet)</p><p>Learning Python Programming Language Through Video Lectures</p><p>8/8</p><p>https://catonmat.net/feed</p><p>https://catonmat.net/bash-vi-editing-mode-cheat-sheet</p><p>https://catonmat.net/learning-python-programming-language-through-video-lectures</p><p>The Definitive Guide to Bash Command Line History</p><p>Listing and Erasing Command History</p><p>History Expansion</p><p>Event Designators</p><p>Word Designators and Modifiers</p><p>Modifying History Behavior</p><p>Tuning the Command Prompt</p><p>Bash History Cheat Sheet</p>

Mais conteúdos dessa disciplina