0
$ echo hello; cd ~/Docs    
hello

$ !! 
echo hello; cd ~/Docs
hello

I want to get hello and execute the change directory after typing !! without re-showing echo hello; cd ~/Docs, again.

Edit:

fc only works for one command, but doesn't execute the full line from terminal. The same for !! or $(!!), they don't work. I tried stty -echo, but the same - it only hides typing !!, but still shows the command. The closest solution I had is to store that command then run it from a script.sh, but it will still show that it's running a script.sh.

1 Answer 1

0

Create the following function that is loaded with your bash config (for instance in .bashrc):

rpt() {
  IFS=';' read -r -a array <<< "$(fc -ln -1)"
  for element in "${array[@]}"
  do
    $element
  done
}

Run the command rpt to execute the function.

This reads the last command string into an array, and splits it at the ; signs, and each element in the array (each command) is run separately.

For this to work, the ; is a reserved character, that can not under any circumstance be used in printed strings etc.

This is a limitation you have to accept for this solution, but I consider it "good enough" to work.

2
  • but only executes one command, not a line from terminal, for example: echo hello; cd ~/Docs;. Now, if you run $( fc -ln -1 ); it will result in executing echo only, resulting in hello; cd ~/Docs;. I want it to execute the full line including the cd command.
    – Johny
    Mar 11, 2021 at 10:05
  • @muru I've edited my answer completely - you might want to remove earlier comments. Mar 11, 2021 at 15:11

You must log in to answer this question.

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