[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Home made backup system



On Thu, Dec 19, 2019 at 09:53:46AM +0100, tomas@tuxteam.de wrote:
> > ...
> > >>   if test -z "$home" -o \! -d "$home" ; then

The main issue here is that the use of the binary -o and -a operators
in "test" or "[" is not portable.  It might work in bash's implementation
of test (sometimes), but you can't count on it in other shells.

The preferred way to write this in a bash script would be:

if [[ -z $home || ! -d $home ]]; then

Or, in an sh script:

if test -z "$home" || test ! -d "$home"; then

or:

if [ -z "$home" ] || [ ! -d "$home" ]; then


> > the backslash is just protecting the ! operator 
> > which is the not operator on what follows.

In the shell, backslash is a form of quoting.  \! is exactly the same as
'!' but it's one character shorter, so you'll see people use the shorter
form a lot of the time.

You don't actually NEED to quote a lone ! character in a shell command.

wooledg:~$ echo hi !
hi !

However, when the ! character is NOT all alone, in bash's default
interactive mode (with history expansion enabled), certain !x
combinations can trigger unexpected and undesired history expansion.

wooledg:~$ set -o histexpand
wooledg:~$ echo hi!!
echo hiset -o histexpand
hiset -o histexpand

So, some people who have run into this in the past have probably
developed a defense mechanism of "always quote ! characters, no matter
what".  Which isn't wrong... but even then, it's not always enough.

History expansion is a bloody nightmare.  I recommend simply turning
it off and living without it.  Of course, that's a personal preference,
and you're free to continue banging your head against it, if you feel
that the times it helps you outweigh the times that it hurts you.

wooledg:~$ set -o histexpand
wooledg:~$ echo "Oh Yeah!.mp3"
bash: !.mp3: event not found

... and then, to add insult to injury, the command with the failed history
expansion isn't even recorded in the shell's history, so you can't just
"go up" and edit the line.  You have to start all over from scratch, or
copy and paste the command with the mouse like some kind of Windows user.


Reply to: