Home > Software engineering >  Why does ''GLOBIGNORE=keepfile rm *'' delete keepfile?
Why does ''GLOBIGNORE=keepfile rm *'' delete keepfile?

Time:02-23

There is something I am not really getting:

touch a b c ; mkdir -p git ; mkdir -p iii/ooo/ppp ; touch git/rtdsfgsdg  ; touch .sdfsadf

So, did that produce what I wanted?

» tree -a
.
├── a
├── b
├── c
├── git
│   └── rtdsfgsdg
├── iii
│   └── ooo
│       └── ppp
└── .sdfsadf

4 directories, 5 files

Yepp. Some files, one dir, one hidden file. All good.

Now I want to delete everything, but I want to keep the hidden file and the git directory. GLOBIGNORE to the rescue!

GLOBIGNORE=git rm -rf *

How did it went?

» tree -a
.
└── .sdfsadf

0 directories, 1 file

Simply awful. Where is my git folder? Why is GLOBIGNORE not working as advertised?

   GLOBIGNORE
          A  colon-separated  list  of  patterns  defining the set of file
          names to be ignored by  pathname  expansion.   If  a  file  name
          matched  by a pathname expansion pattern also matches one of the
          patterns in GLOBIGNORE, it is removed from the list of matches.

I am on:

» bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3 : GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

CodePudding user response:

The syntax you're using to temporarily apply the variable only applies at execution time, not at parse time; consequently, it can't change how the glob is expanded, because replacing the * with a list of files happens before rm is invoked.

To modify parse-time behavior, split into two separate commands:

GLOBIGNORE=git
rm -rf *

See this running successfully at https://replit.com/@CharlesDuffy2/LovablePoliticalPrograms

  •  Tags:  
  • bash
  • Related