8 easy ways to reuse commands on Linux

You can move to the beginning of the command using ^a or to the end using ^e. In the example below, I forgot to use the echo command and then went back to the beginning of the command with ^a and inserted the command.

$ Have a happy and very profitable day!
bash: Have: command not found...
$ echo Have a happy and very profitable day!

You can also use the left and right arrow keys to move left and right. Move back to the command with the up arrow key, and move left and right as needed. Once you’ve made your changes, press the return key to run the command you’ve just modified.

^a moves cursor to the beginning of the command, and ^e moves the cursor to the end of the command.

5. Use only the last string in the previous command

If you want to reuse the final argument from the previous command, you can refer to it as !$ in the next command. Here is an example:

$ echo this is not a test
this is not a test
$ echo !$
echo test
test

6. Use a reverse search to find a command you’ve run previously

To find a command in your command history that you want to rerun, you can hold the Ctrl key and press r. You will then be prompted to enter the search text. The most recent command containing that string will be run. The string can include more than a single word. The text of the original command will appear to the right as you type.

(reverse-i-search)`happy': echo Have a happy and very profitable day!
(reverse-i-search)`very profitable': alias byebye="echo "Have a happy and very profitable day!""

7. Turn complex commands into aliases

When you run a command that you’re likely to use frequently, you might consider turning it into an alias. You can create an alias with a command like the one below, but this will only keep it working until you log out. Add it to your .bashrc file to make it available every time you log in. Here are some examples:



Source link