Bash Tricks
===Keyboard Shortcuts=== {| | align=”center” style=”background:#f0f0f0;”|’'’Ctrl + A’’’ | align=”center” style=”background:#f0f0f0;”|’'’Go tobeginning of line’’’ |- | Shortcut||Function |- | Ctrl + E||Go to end of line |- | Ctrl + U||Clear all characters to the left of the cursor |- | Ctrl + R ||Search through command history |- | Ctrl + C||Kill whatever you are running |- | Ctrl + Z||Background current process. fg restores it. |- | Ctrl + W||Delete the word before the cursor |- | Ctrl + K||Clear all characters to the right of the cursor |- | Esc + T||Swap the last two words before the cursor |- | Alt + F||Move cursor forward one word on the current line |- | Alt + B||Move cursor backward one word on the current line |- | Alt + .||Cycle through arguments used in previous commands | |} ===Redirecting Output=== Redireting output in many cases is as simple as UNIX/DOS:
find ~ -name test >outputfile
This will only redirect the standard output, to redirect standard error:
find ~ -name test 2>erroroutputfile
To redirect the output of both standard output AND standard error:
find ~ -name test >outputwitherrorsfile 2>&1
===Eliminate ‘loser takes all’ Bash History=== Bash only writes the command history on exiting the shell. If multiple sessions are open simultaneously the very last session to be closed will overwrite any command history from other sessions since it was opened. Confused? try the following: #Opening a shell and type echo 1;echo 2 (session number 1.) #Keep this session open and open another bash session (session number 2) and use the history command and you will see that the last two commands issued are not there, as the commands are only written on exiting the shell. But that it not all… #Close session 1 and reopen another session (session number 3) and you will now see the echo 1;echo 2 #Close session number 2 and reopen another session (session number 4) and you will now see that echo 1;echo 2 has been lost! ====How to fix==== #Bash to save to the history upon every command, not when the session exits #Bash must always append instead of overwriting command history. #The history command must re-read the newlines upon running it, by default it only reads it when the session is started.
*Add the following to .bashrc:
PROMPT_COMMAND='history -a' shopt -s histappend alias history='history -n;history'
===Refer to previous dirs and commands===
Refer to the last dir within a command with: <pre>~-/</pre> Insert the last command entered with: <pre>!!</pre> If, for example sudo is not used when it should have been sudo !! can be used as an alternative to retyping/copying/pasting the last command.
Insert all the arguments from the previous command with
!*
Refer to the Nth argument of the last command:<pre>!!:n</pre> Refer to the first argument:<pre>!!:2</pre> Refer to the last argument:<pre>!:$</pre>
===Dir navigation===
cd with no arguments always goes to your home dir.
cd to last dir: cd -
Put current dir in the stack pushd . Then go back to it with popd
Go to dir, run a command and then return to the current dir: (cd dir && command)
===Scrolling ls listings made easier=== Instead of ls /etc|less this can be shortened to less /etc . If a folder is specified in less it will ls is and less the contents. ===Simple calculator=== Only useful as a simple calculator as an alternative to using a full caclualator like bc -l; fractions are decimals are not supported:
$ echo $[16*2] 32 $ echo $[100/11] 9
==See Also== *[http://www.gnu.org/software/bash/manual/html_node/index.html Bash Reference Manual] *[http://www.linuxjournal.com/article/7385 My Favorite bash Tips and Tricks] *[http://www.ukuug.org/events/linux2003/papers/bash_tips/ Bash Tips & Tricks - Simon Myers - UKUUG Linux 2003 Conference • August 2003] *[http://twitter.com/bashtips Bashtips on twitter] *[http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history The Definitive Guide to Bash Command Line History]
[[Category:Linux]]