The OS X Terminal is a commonly used utility for troubleshooting OS X, because it allows you to run custom commands and scripts for looking up information and adjusting system settings. While useful, the Terminal is intended primarily for power users who have an understanding of the UNIX underpinnings in OS X.
Often when running commands in the Terminal, various iterations of the command will be executed to try different settings or perform slightly different tasks, which in the Terminal can be frustrating as it can require you type out sometimes extensively long commands over and over again.
For instance, if you are monitoring a variable of a small configuration file, you might run a command similar to the following to read through the contents of a file and print out the lines that contain your variable:
cat /etc/configfile.conf | grep myVariable
As you use your system, you can repeat the above command to keep printing the line of the configuration file and see if and when it changes when performing another task (altering settings in the system preferences, running programs, etc.). While you can type out the command over and over again, there are some options you can perform to run the command again:
- Command repetition
The default bash shell used in OS X supports the use of two exclamation points (!!), which if entered will simply run the previous command again. Therefore, after entering your command you can just type the two exclamation points followed by Enter, and the command will run again as you initially entered it.
- Command re-entry
In addition to the two exclamation points, you can make use of the Bash history using arrow keys. The Bash shell stores a list of the previously entered commands, and if you press the up arrow, then you will display the previous command, or be able to go further back in the history by continuing to press the up arrow. When the command you wish to run presents itself, you can then execute it. This option is convenient if you wish to alternate between two common commands in order to run your specific task.
- Command substitution
Another option for rerunning commands is to use command substitution, in which you can run the previously entered command but change one aspect of it, which is commonly done. For instance, if you wish to remotely log into a server, then you can run the following command:
This command will log you in using SSH to manage the specified server using the command line; however, you might wish to exit your SSH session and then access this same server again using sftp to download a file, which would be done similarly by the following command:
Since both of these commands are identical except for the "ssh" and "sftp" part, you can use the command re-entry option mentioned above to access the previous command and then move your cursor to the "ssh" component and edit; however, this can be time-consuming. Instead, you can use a command substitution option that is supported in Bash, which will rerun the previous command but allow you to switch "ssh" with the desired "sftp."
To do this, after having exited your "ssh" session, run the following syntax using the caret character, where the text in the command following the first caret will be replaced by that following the second caret:
The result of this will be to access the same server with "sftp firstname.lastname@example.org," and not require you use arrow keys to navigate and change command components. Furthermore, this syntax will not be stored in the Bash history, which instead will store the altered version of the command.
This substitution syntax has numerous uses, including changing specified boolean values from false to true or vice-versa (as is commonly done using the "defaults" command), or by changing designated command flags that may be in the middle or toward the front of a typed command. It can also be used to fix typos that you might have entered in a command. For instance, if you ran a command to target a file in the global Library folder (/Library) but instead targeted your user library with the tilde symbol (~/Library), you can remove the tilde and rerun the command by simply typing "^~^" in the Terminal.