Bash's exclamation

The exclamation mark has multiple uses in BASH, says google. It also says that in command line a statement like

echo "Hello world!" 

would give

 bash: !": event not found 

. If one writes it like

echo "Hello world! " 

, with space after “!”, it is correctly displayed.
I understand that double quotes give literal meaning to almost everything quoted (the textbooks say: except for “$”, “`”, and the escape character). On the other hand, the interpretation of “!” in command line is different from its interpretation in a simple bash script

#!/bin/bash echo "Hello world!"

. No space needed after “!”, and the execution of the one-statement script gives no syntax error. I’m not sure why “!” is interpreted by bash differently on command line (when it points to bash history, as “!!” executes the last command in history, whereas “!” followed by a number executes a specific command from bash history) than in a script in which it appears. Is it “just like that”, i.e. the standard that should not be questioned? :slight_smile:

Hi
Don’t use double quotes;


echo 'Hello world!'


Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 11.4 (x86_64) Kernel 2.6.37.6-0.9-desktop
up 2 days 9:01, 4 users, load average: 0.01, 0.11, 0.13
GPU GeForce 8600 GTS Silent - Driver Version: 290.10

If you read the lengthy man page you will see:

HISTORY EXPANSION
The shell supports a history expansion feature that is similar to the
history expansion in csh. This section describes what syntax features
are available. This feature is enabled by default for interactive
shells, and can be disabled using the +H option to the set builtin com-
mand (see SHELL BUILTIN COMMANDS below). Non-interactive shells do not
perform history expansion by default.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Exactly. Double-quote imply you want variable (and command)
interpolation. That people use them all the time either means they want
that functionality or they are not familiar with the reason for
double-quotes. For literal strings always use single-quotes.

Good luck.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJO4TsAAAoJEF+XTK08PnB5RIgP/0cKxcOo2vNTlTqZx6GYE8SV
8kKbwfQ0yay6J6WmPZPwBbedw69h9X/PUg6dN+znu0vSRyiUmTKZuW8bpAN0iZOX
7YcsGBAmvCtMY50FDAm+j6ZnMt3dKZJCHPqGXdpPjTVmI0RGaaHLx6S5mbFCrti0
4IE5YqRqcAWz/OptAx8U/6Lbr82DHfrLy3COGLaCtmShE3LkijYGAlFu8x63HflD
Lb6sq7StThmZnQblv1cIRAv0x0/186XNNgj5w48c/fVJb+3nKbJrExJiST4oI3oO
fSStv56G4xsLu00K7U5XoKFZHAACdQuTQGzym8KzlgrotLpt7naZ5KNSssJVxjh8
txzxOKMmJ86aznC72nqK+o3heF4w7uL30oVGYvz9MBLNeai4+xGZB/OPB9MsUXzK
tCcI4VSt04q34ZsMwv0+Tvuuz/NZC4R45FS3jVcS50AoJ+0RXZ/KVBNbFTgp7Ore
CcEY8aU2X/ggownPao6/N1O08wWuJ36xr27I1ZCxEN5k8CsvF7VoxXfdfLghps5E
SY0LUojLDuYNbhbJfOr19IB5wdm3UDcSZriaQiROOgJVuHkNJs1b+DGvjgRWkcyO
HCmdtU5wbLnZEfKTC50t49YXW+D85I0bPFtrKmVyBeuTMKSWbFHZCMIG/eqw/0Yk
ioEJNSffIBrWoqRd2sJC
=ToFW
-----END PGP SIGNATURE-----

Thank you, all.