Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does ''GLOBIGNORE=keepfile rm *'' delete keepfile?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading