0

I am trying to write a basic bash script where I wish to create some files in my directory and then assign them to variables i.e file to keep as "FILE_KEEP" and file to delete as "FILE_DELETE". How do I assign files to these variables? and How to I remove them using rm command?

enter image description here

touch a.sh b.sh c.sh d.sh e.sh f.sh g.sh
file_keep=(a.sh b.sh c.sh)
file_delete=(d.sh e.sh f.sh)

rm $file_delete
Dan Bonachea
  • 2,408
  • 5
  • 16
  • 31
  • 1
    For one, you're typing the variable name wrong (`FILE_DLETE` is not `FILE_DELETE`). For another, you aren't using array expansion syntax (`rm -f -- "${FILE_DELETE[@]}"`). Also, note that all-caps names are used for variables meaningful to the shell itself; per POSIX-defined conventions, application-defined variables should have at least one lowercase character in their names. – Charles Duffy Mar 18 '22 at 22:26
  • 1
    See https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html: *The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities* -- to provide a concrete illustration of why this guideline exists, `for PATH in /dir/*/` will break your shell because it overrides the PATH, but `for path in /dir/*/` works fine. That applies to both regular and environment variables because they share a single namespace. – Charles Duffy Mar 18 '22 at 22:28
  • This looks dangerous as well without using absolute paths or a defined system `$PATH` -- I am always extra careful when building scripts to remove files . If you misname something, or simply have an empty variable where `rm /var/www/a.sh` could end up being `rm /var/www` which obviously wouldn't execute without `-rf` but it's still a scary thought. – Zak Mar 18 '22 at 22:33

0 Answers0