I have a bash script that uses grep to screen out file with certain extensions and under certain directories. The script looks something like follows:
generate_exclud_extensions() {
echo "\"(cpp$"
}
generate_exclude_dirs() {
echo "|^test)\""
}
command=$(generate_exclud_extensions)$(generate_exclude_dir)
echo $command
git ls-files | grep -vE $command
This script was supposed to screen the files with cpp extension, AND files under test directory. However the result only screened out the first, had the second remained.
Fact 1: echo $command outputs "(cpp$|^test)", and running the full command git ls-files | grep -vE ""(cpp$|^test)"" in terminal, I got the correct result.
Fact 2: Tried using grep twice as follows:
git ls-files | grep -vE "cpp$" | grep -vE "^test"
same wrong result in bash script but correct in terminal.
I've been searching but didn't have an luck yet. I appreciate it if someone could shed some light on this.
git ls-filesprovides possibility of adding an exclusion pattern--exclude=<pattern>commandis assignedgenerate_exclude_dirbut you definedgenerate_exclude_dirs().