'Grep and other programs not found in script [duplicate]

I am trying to gather all of my TODOs and make one file out of them.

#!/bin/bash

# Script for getting all TODOS

PATH="~/documents/obsidian-notes/"
OUTPUT="/home/fish/documents/obsidian-notes/TODO.md"

echo "#TODO" > $OUTPUT

grep -hr --exclude-dir=plugins "\bTODO\b:.*" $PATH | awk '{print $0,"\n"}' >> $OUTPUT

If I run each line in my prompt it works perfectly. When I add them to a script and make it executable and run it I get

./obtodo.sh: line 10: grep: command not found
./obtodo.sh: line 10: awk: command not found
./obtodo.sh: line 12: chown: command not found

I tried running as sudo and I made the script executable with chmod a+x



Solution 1:[1]

$PATH is a special variable to the shell. It defines the list of directories to be searched when executing subcommands.

Bash won't know where grep, awk or chown are. Please use different variable name instead of $PATH.

Try

#!/bin/bash

# Script for getting all TODOS

xPATH="~/documents/obsidian-notes/"
OUTPUT="/home/fish/documents/obsidian-notes/TODO.md"

echo "#TODO" > $OUTPUT

grep -hr --exclude-dir=plugins "\bTODO\b:.*" $xPATH | awk '{print $0,"\n"}' >> $OUTPUT

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nemesis