Bash History: Correct / Repeat The Last Command With a Substitution

www.lau‮‬tturi.com
Bash History: Correct / Repeat The Last Command With a Substitution

To correct or repeat the last command in Bash with a substitution, you can use the !! command followed by the :s option and a regular expression. This will run the last command again, but it will replace the specified pattern with the replacement string that you provide.

Here is an example of how to use the !!:s command to correct the last command:

# Run a command
echo "hello world"

# Incorrect the command by replacing "hello" with "goodbye"
!!:s/hello/goodbye/

In this example, the echo command is run first, and it outputs the string "hello world". Then, the !!:s command is used to correct the last command by replacing the string "hello" with the string "goodbye". This will run the echo command again, but with the substitution applied, so it will output the string "goodbye world".

You can also use the !!:s command to repeat the last command with a substitution, without making any changes to the original command. For example:

# Run a command
echo "hello world"

# Repeat the last command with a substitution
!!:s/world/universe/

In this example, the echo command is run first, and it outputs the string "hello world". Then, the !!:s command is used to repeat the last command with a substitution, replacing the string "world" with the string "universe". This will run the echo command again, but with the substitution applied, so it will output the string "hello universe".

Note that the !!:s command only works with the last executed command.

Created Time:2017-10-16 14:38:38  Author:lautturi