How to repeat commands in OS X Terminal

Author Topic: How to repeat commands in OS X Terminal  (Read 1879 times)

Offline mhasan

  • Faculty
  • Full Member
  • *
  • Posts: 148
    • View Profile
How to repeat commands in OS X Terminal
« on: June 20, 2012, 01:44:22 AM »
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," 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.
MM Hasan
Sr. Lecturer
Department of CSE
Daffodil International University
                           (Please don't print this post unless you really need it)