Find shell scripts
Q: How do I recursively find all shell scripts in my current working directory?
A: Search for the hash-bang line:
$ git -c grep.fallbackToNoIndex=yes grep -lPe '\A#!\s*/bin/([bd]?a)?sh\b'
Explanation
Iām using Perl Compatible Regular Expressions (PCRE) as this gives us \A
to match at the beginning of the subject.
The circumflex (^
) would match each line, not only the first line.
Then #!/bin/sh
plus any other shell like Almquist shell ash
, Debian Almquist Shell dash
and GNU Bourne-Again SHell bash
.
You might want to also match C-Shell csh
, TENEX C-Shell tcsh
and Z-Shell zsh
.
There may be blanks between #!
and /bin/sh
.
Instead of git grep
you also can use grep -r
itself, but as I have many git repositories, git grep
is more efficient as it can use the index.
Issues
This will not find shell libraries, e.g. shell scripts not starting with a hash-bang line.