Andy
Andy

Reputation: 950

\! In Bash Commands

What is the function of \! in a bash command?

For example, I notice that this command below will differ in behavior with \!:

find subdir -lname 'test*' \! -newer somethinghere

Google searching does not appear to bring up anything specifically related to \! except for command line formatting.

Upvotes: 1

Views: 2318

Answers (2)

Picaud Vincent
Picaud Vincent

Reputation: 10982

The ! is for negation, for instance:

find . -readable 

will return all readable files/directories, whereas

find . ! -readable

will return all files/directories that can not be read (broken link or -r permission)

The \ char is for escaping.

However, curiously on my computer (Debian/bash) I need to escape the * char but not the ! one. To be more explicit I need to write

 find . -name abc\*

but

 find . -name abc\* ! -readable
 find . -name abc\* \! -readable

work and have same effect

Upvotes: 2

Charles Duffy
Charles Duffy

Reputation: 295383

Putting a backslash before a character escapes it, preventing it from being parsed as syntax. Thus, \! is a terser equivalent to '!' -- it ensures that find is passed ! as an argument, without the shell interpreting it in any way.

Without either of these, if [[ $- = *H* ]] (which is true in an interactive shell and by default), and if histchars is unmodified from its default value, ! triggers history expansion. This is per the bash man page's QUOTING section:

When the command history expansion facilities are being used (see HISTORY EXPANSION below), the history expansion character, usually !, must be quoted to prevent history expansion.

Upvotes: 3

Related Questions