Please look at these commands:
$ notify-send SYNC TIME!
$ notify-send 'SYNC TIME!'
$ notify-send "SYNC TIME!"
bash: !": event not found
$
The first two commands produce a notification bubble as expected. The third gives the error shown.
and
$ echo SYNC TIME!
SYNC TIME!
$ echo 'SYNC TIME!'
SYNC TIME!
$ echo "SYNC TIME!"
bash: !": event not found
$
Here as well, the echo
works for first two commands but not in the third.
More problems here (although I was not planning on using this): both notify-send "SYNC!TIME"
and echo "SYNC!TIME"
give bash: !TIME": event not found
.
But both notify-send
and echo
work with "SYNC! TIME"
Can someone please explain why the bash: !": event not found
error appears?
!
is the default history expansion character in Bash, see the section "HISTORY EXPANSION" in the Bash manpageHistory expansion doesn't take place if the
!
is enclosed by single quotes, as inHistory expansion doesn't take place if the
!
is followed by a space, tab, newline, carriage return, or=
, as inHistory expansion does take place in
So you'll get an error if there isn't a command starting with
"
in your historyBecause in bash,
!
is a reserved word (OK, character), it has special meaning in different contexts. In this particular case, you are falling afoul of its significance in history searching. Fromman bash
:Basically, what this means is that bash will take the characters after the
!
and search your history for the first command it finds that starts with those characters. It is easier to demonstrate than explain:The
!
activated history expansion, which matched the first command starting withe
which was the previously runecho foo
which was then run again. So, when you wrote"SYNC TIME!"
, bash saw the!"
, searched history for a command starting with"
, failed and complained about it. You can get the same error by running, for example!nocommandstartswiththis
.To print an exclamation mark, you need to escape it in one of these two ways: