Find "secure file erase" commands on the system This code finds which &quot;secure file erase&quot; command exists on the system.  It used to simply test for &quot;shred&quot; and pick &quot;rm&quot; as a default.  Now it is a bit more complex since I've ported the code to operating systems with &quot;srm&quot; and &quot;rm -P&quot;.  Is this still cleanly written?  How can I improve it?
var shredPath, shredFlag string // Memoization cache

// shredCmd determines which command to use to securely erase a file. It returns
// the command to run and what flags to use with it. Determining the answer
// can be slow, therefore the answer is memoized and returned to future callers.
func shredCmd() (string, string) {
    // Use the memoized result.
    if shredPath != &quot;&quot; {
        return shredPath, shredFlag
    }

    var path string
    var err error
    if path, err = exec.LookPath(&quot;shred&quot;); err == nil {
        shredPath, shredFlag = path, &quot;-u&quot;
    } else if path, err = exec.LookPath(&quot;srm&quot;); err == nil {
        shredPath, shredFlag = path, &quot;-f&quot;
    } else if path, err = exec.LookPath(&quot;rm&quot;); err == nil {
        shredPath, shredFlag = path, &quot;-f&quot;
        // Does this command support the &quot;-P&quot; flag?
        tmpfile, err := ioutil.TempFile(&quot;&quot;, &quot;rmtest&quot;)
        defer os.Remove(tmpfile.Name()) // clean up
        err = RunBash(&quot;rm&quot;, &quot;-P&quot;, tmpfile.Name())
        if err != nil {
            shredFlag = &quot;-Pf&quot;
        }
    }

    // Single exit, so we don't have to repeat the memoization code.
    return shredPath, shredFlag
}

// RunBash runs a Bash command.
func RunBash(command string, args ...string) error {
    cmd := exec.Command(command, args...)
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Start()
    if err != nil {
        log.Fatal(err)
    }
    err = cmd.Wait()
    if err != nil {
        return fmt.Errorf(&quot;RunBash cmd=%q err=%w&quot;, command, err)
    }
    return nil
}
```
You have no synchronization for these package variables
var shredPath, shredFlag string

Therefore,
fmt.Println(shredPath, shredFlag)
go func() {
    for i := 0; i &lt; 1000; i++ {
        shredCmd()
    }
}()
for i := 0; i &lt; 1000; i++ {
    shredCmd()
}
fmt.Println(shredPath, shredFlag)

WARNING: DATA RACE
Found 3 data race(s)

os.Remove() and commands may fail if the file is open.
If you encounter something unexpected, for example Windows, you do nothing, not even os.Remove().
The if statements are ugly. Since shredCmd() is effectively a test, use the standard testing package technique, table-driven tests.
You provide no context for usage or testing, for example
func ShredFile(filename string) error {
    // ...
}

And so on.

Here's an attempt to fix some of the issues:
shred.go:
package main

import (
    &quot;errors&quot;
    &quot;fmt&quot;
    &quot;io/ioutil&quot;
    &quot;os&quot;
    &quot;os/exec&quot;
    &quot;time&quot;
)

var shredCmds = []struct {
    name, opts string
}{
    {&quot;shred&quot;, &quot;-u&quot;},
    {&quot;srm&quot;, &quot;-f&quot;},
    {&quot;rm&quot;, &quot;-Pf&quot;},
    {&quot;rm&quot;, &quot;-f&quot;},
    {&quot;sdelete&quot;, &quot;-a&quot;},
}

func shredTemp(path, opts string) error {
    file, err := ioutil.TempFile(&quot;&quot;, &quot;shredTemp.&quot;)
    if err != nil {
        return err
    }
    filename := file.Name()
    defer os.Remove(filename)
    defer file.Close()

    err = file.Close()
    if err != nil {
        return err
    }
    err = RunCmd(path, opts, filename)
    if err != nil {
        return err
    }
    return nil
}

var shredPath, shredOpts = func() (string, string) {
    for _, cmd := range shredCmds {
        path, err := exec.LookPath(cmd.name)
        if err != nil {
            continue
        }
        err = shredTemp(path, cmd.opts)
        if err == nil {
            return path, cmd.opts
        }
    }
    return &quot;&quot;, &quot;&quot;
}()

func ShredFile(filename string) error {
    fi, err := os.Stat(filename)
    if err != nil {
        return err
    }
    if !fi.Mode().IsRegular() {
        err := errors.New(&quot;filename is not mode regular&quot;)
        return err
    }

    if shredPath == &quot;&quot; {
        return os.Remove(filename)
    }

    err = RunCmd(shredPath, shredOpts, filename)
    if err != nil {
        return err
    }
    return nil
}

func RunCmd(command string, args ...string) error {
    cmd := exec.Command(command, args...)
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Start()
    if err != nil {
        return fmt.Errorf(&quot;RunCmd cmd=%q err=%w&quot;, command, err)
    }
    err = cmd.Wait()
    if err != nil {
        return fmt.Errorf(&quot;RunCmd cmd=%q err=%w&quot;, command, err)
    }
    return nil
}

func main() {
    file, err := ioutil.TempFile(&quot;&quot;, &quot;ShredFile.&quot;)
    if err != nil {
        fmt.Println(err)
        return
    }
    filename := file.Name()
    file.Close()
    defer os.Remove(filename)
    start := time.Now()
    err = ShredFile(filename)
    since := time.Since(start)
    fmt.Println(filename, since, err)
}