
Handy tips and tricks for bash. Got suggestions? @bashtips them to us
/bin/bash
Joined July 2009
- Tweets26
- Following6
- Followers1.6K
- Likes0
*tap tap* Is this thing still on? Looks like we should start posting tips again. If you'd like to help out, say hello!
Need to run a command on multiple servers simultaneously? alias dsh="cat ~/nodes.txt | xargs -P0 -I NODE ssh NODE -C"
Adding bind '"\e[A"':history-search-backward and bind '"\e[B"':history-search-forward to .bashrc makes your up/down keys autocomplete.
Use ~-/ to refer to the last directory you were in: "cd woo/; cd ../yay/; cp ~-/foo.txt .;" will copy foo.txt from woo/ to yay/
Use C-v [KEY] to insert a literal character. E.g. C-v [TAB] to insert a tab rather than auto-completing - useful when grepping logs.
Add "shopt -s checkwinsize" to your .bashrc to make sure terminals wrap lines correctly after resizing them.
Use bash expansion when cp/mv'ing: `cp file{,.bk}` to create "file.bk", `mv file{.bk,}` to replace "file" with "file.bk"
3
Insert the last argument of the previous command: !!$. Eg: `touch foo; ls -la !!$` (From @zigdon)
Add "shopt -s cdspell" to your .bashrc for small typos to be automatically corrected when changing directory.
curl -u <username> http://teapawt.org/statuses/update.xml -d status="Tweet from the cmd-line without exposing your password in history"
Both replace terms in the last cmd run, so if you'd just run "echo foofoo" the output would be "barfoo" and "barbar" respectively.
Someone asked the difference between ^foofoo^bar and !!:gs/foofoo/bar? The former only replaces the first match, the later all matches.
Add "shopt -s histappend" and "export PROMPT_COMMAND='history -a'" to your .bashrc and your command history will be saved for all terminals.
Repeat the entire last command with "!!", handy for running the last command as root, for example "sudo !!"
Add "bind Space:magic-space" to your .bashrc and any ! combinations will be auto-expanded when you hit space.