Run a list of commands in a script

Trying to run this and get no such file or folder even though the individual files work in terminal.

#!/bin/bash

pkill Microsoft Teams

rm -r ~/Library/Application\ Support/Microsoft/Teams/blob_storage/*
rm -r ~/Library/Application\ Support/Microsoft/Teams/Cache/*
rm -r ~/Library/Application\ Support/Microsoft/Teams/databases/*
rm -r ~/Library/Application\ Support/Microsoft/Teams/GPUCache/*
rm -r ~/Library/Application\ Support/Microsoft/Teams/IndexedDB/*
rm -r ~/Library/Application\ Support/Microsoft/Teams/Local Storage/*

echo "Clearing Microsoft Teams Cache"

I know this must be basic stuff but not a Mac user normally.


Solution 1:

Quoted Spaces

A couple of lines need quoting to deal with spaces:

pkill Microsoft Teams

Should read:

pkill "Microsoft Teams"

and Local Storage needs a slash to become Local\ Storage.

Absolute Paths

If you continue to encounter problems, try expanding the ~ and use absolute/full paths. This can solve many problems and allows scripts to be run from within other tools like cron or launchd.

ShellCheck

A useful tool to know about is ShellCheck. This online or offline tool can help point out common shell script problems.