Bash Shortcuts and Tips
Repeating an argument
You can repeat the last argument of the previous command in multiple ways. Have a look at this example:
$ mkdir /path/to/dir
$ cd !$
The second command might look a little strange, but it will just cd to /path/to/dir.
Some keyboard shortcuts for editing
There are some pretty useful keyboard shortcuts for editing in bash. They might appear familiar to Emacs users:
• Ctrl + a => Return to the start of the command you’re typing
• Ctrl + e => Go to the end of the command you’re typing
• Ctrl + u => Cut everything before the cursor to a special clipboard
• Ctrl + k => Cut everything after the cursor to a special clipboard
• Ctrl + y => Paste from the special clipboard that Ctrl + u and Ctrl + k save their data to
• Ctrl + t => Swap the two characters before the cursor (you can actually use this to transport a character from the left to the right, try it!)
• Ctrl + w => Delete the word / argument left of the cursor
• Ctrl + l => Clear the screen
Redirecting both Standard Output and Standard Error:
# ls -ltR 2>&1 > /tmp/temp.txt
Specify this in .bashrc
Make Bash append rather than overwrite the history on disk:
# shopt -s histappend
Whenever displaying the prompt, write the previous line to disk:
# export PROMPT_COMMAND=’history -a’
To erase duplicate entries in History.
# export HISTCONTROL=erasedups
(or)
# export HISTCONTROL=ignoreboth
To see the history with timestamps
# export HISTTIMEFORMAT=”%d/%m/%Y-%H:%M:%S ”
To set the Size of the historyHISTSIZE: The number of commands to remember in the command history. The default value is 500.
# export HISTSIZE=500
Searching the Past
- Ctrl+R searches previous lines
This will put bash in history mode, allowing you to type a part of the command you’re looking for. In the meanwhile, it will show the most recent occasion where the string you’re typing was used. If it is showing you a too recent command, you can go further back in history by pressing Ctrl + r again and again. Once you found the command you were looking for, press enter to run it.
- !tcp will execute the previous command which starts with “tcp”
Recent Comments