I need to run a command, and then run the same command again with just one string changed.

For example, I run the command

$ ./myscript.sh xxx.xxx.xxx.xxx:8080/code -c code1 -t query

Now from there, without going back in the command history (via the up arrow), I need to replace code1 with mycode or some other string.

Can it be done in Bash?

  • Why do you need it?
    – choroba
    Commented Nov 16, 2015 at 13:44
  • may the url contain other b ?
    – lese
    Commented Nov 16, 2015 at 13:44
  • Can you please clarify how you get the initial string in input. From your post it seems it is on your terminal likewise you typed it as a command
    – lese
    Commented Nov 16, 2015 at 14:03
  • Raja... a variation of echo http://xxx.xxx.xxx.xxx/a/b/b/c/d/e | sed 's/b/c/g'? Please tell what do you want to do, execute a command,call a program that will reach that url, change a variable value...?!?
    – Hastur
    Commented Nov 16, 2015 at 14:15
  • See unix.stackexchange.com/questions/116623/… for fc-s. Commented Sep 3, 2020 at 13:46

2 Answers 2


I renamed your script, but here's an option:

$ ./myscript.sh xxx.xxx.xxx.xxx:8080/code -c code1 -t query

after executing the script, use:

$ ^code1^code2

... which results in:

./myscript.sh xxx.xxx.xxx.xxx:8080/code -c code2 -t query

man bash and search for "Event Designators":


Quick substitution. Repeat the last command, replacing string1 with string2. Equivalent to !!:s/string1/string2/

Editing to add global replacement, which I learned just now from @slm's answer at https://unix.stackexchange.com/a/116626/117549:

$ !!:gs/string1/string2

which says:

!! - recall the last command
g - perform the substitution over the whole line
s/string1/string2 - replace string1 with string2
  • 9
    Of course you use !! to repeat the previous command in its entirety, unaltered, but almost anything that begins with !!: can be shortened to !:; e.g., !:gs/string1/string2. Commented Jan 21, 2016 at 19:48
  • 4
    The ^ syntax works in zsh as well. Just documenting for posterity. Commented Jun 23, 2017 at 19:24
  • 8
    ^foo^bar^:G for global search and replace.
    – HappyFace
    Commented Sep 3, 2018 at 10:48
  • 2
    @HappyFace "^foo^bar^:G for global search and replace." Doesn't work for me in Bash 4.4.19. Is that a zsh syntax?
    – wisbucky
    Commented Jul 29, 2020 at 18:49
  • 1
    @wisbucky Yeah, it's zsh.
    – HappyFace
    Commented Jul 29, 2020 at 23:18

The bash builtin command fc can be used to find a command in the history and optionally edit/run it. Use the bash builtin help fc for more documentation.

fc -s code1=code2

Will find the all occurrences of code1 in the last command and change it to code2, then execute the new command.

It can be useful when multiple special characters need to be changed. Suppose the previous command was;

$ java a/b/c/d

# Then,

fc -s /=.

# will produce
$ java a.b.c.d
  • Sometimes taking a little bit more time to add an explanation and where to find further information, will change an difficult answer to understand into something easier to read/understand and adds value to the set of answers.
    – X Tian
    Commented Oct 18, 2021 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .