0

This question already has an answer here:

If I do this in a shell, I can see the !! bash variable contains the last command.

$> echo foo
foo
$> echo !!
echo echo foo
echo foo

But this script

#!/bin/bash

mkdir /path/doesnt/exist ||
{
    echo "Could not !!";
    exit 1;
}

outputs

mkdir: cannot create directory ‘/path/doesnt/exist’: No such file or directory
Could not !!

I expected the output to be Could not mkdir /path/doesnt/exist

Why doesn't this work and how can I fix it?

marked as duplicate by muru, GAD3R, Kusalananda bash Jun 20 at 9:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Because !! is a command which read bash history, and bash history is used only in interactive mode.

Really you don't want that every script will fill up your command history.

From bash manual, section "HISTORY EXPANSION"

Non-interactive shells do not perform history expansion by default.

Not the answer you're looking for? Browse other questions tagged or ask your own question.