Bash Command Line Pro Tips
Rate it:
Open Preview
Read between November 22 - November 22, 2017
23%
Flag icon
To enable programmable completion on an Ubuntu system, source /etc/bash_completion.  You can add this command to your personal initialization files or you can enable it globally by uncommenting the the appropriate lines in /etc/bash.bashrc.
26%
Flag icon
 Your previous working directory is stored in an environment variable named $OLDPWD.
34%
Flag icon
 Time and time again I find myself needing to run another command against the last item on the previous command line.  To access that item in your current command, use "!$".
41%
Flag icon
 For example, to run the last command that started with a "d", type "!d<ENTER>."  Specify as much of the string to make it unique.  If you have several commands in your recent history that begin with "d", you can add additional characters like "!du" or "!df".
47%
Flag icon
 You can perform command substitution by using backticks (`) to surround a command or a dollar sign followed by parenthesis that surround a command.  Surrounding a command with backticks is the older style and is being replaced by the dollar sign syntax. ...more
52%
Flag icon
When I need to perform the same action for multiple items, I use a for loop right from the command line.
58%
Flag icon
 Luckily it's really easy to repeat the previous command with superuser privileges.  If sudo is configured on your system, run sudo !!, otherwise run su - c "!!".
64%
Flag icon
 To repeat the previous command while substituting a string run "^string1^string2^".  This will repeat the previous command using string2 in place of string1.
66%
Flag icon
By default, only the first occurrence of "string1" is replaced.  To replace every occurrence, append ":&".
69%
Flag icon
 You can omit the trailing caret symbol, except when using ":&".
71%
Flag icon
The "!#" string represents the current command line.  To reuse an item on the same command line follow "!#" with ":N" where N is a number representing a word.  Word references are zero based, so the first word, which is almost always a command, is :0, the second word, or first argument to the command, is :1, etc.
75%
Flag icon
Note that if you were to log out and log back in, your aliases would be lost. To make them persist between sessions add them to one of your personal initialization files, sometimes called "dot files," like .bash_profile.