0

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
spraff
  • 911
  • 4
  • 14
  • 29

1 Answers1

1

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.