Issue
I have the following working regex that I tested in regex101
^function\s(\w+)\(\)\s{$
for the following test string
function hello() {
echo "Hello World"
}
function get_aws_credentials() {
echo "Getting AWS Credentials
}
My goal is to get all function names defined in that file and that's what my original regex does, but not in bash.
The problem is that it doesn't work in bash or zsh (I really only care about bash though).
I have researched it and saw some alternatives, like replacing the \w+
for [[:alnum:]]
, but that didn't work either.
This is how I'm testing it: cat utils.sh | grep "^function\s(\w+)\(\)\s{$"
Any idea of what I'm missing here?
Thanks!
Solution
Would this work or are you trying to create a universal regex for every file type?
# Get all function names defined in a bash script file
# Usage: script.sh <script_file>
function get_functions() {
local FUNC_NAME=$(basename $1)
local FUNC_NAMES=$(grep -o '^function [a-zA-Z0-9_]*' $1 | sed 's/function //')
echo "$FUNC_NAMES"
}
Answered By - PCDSandwichMan Answer Checked By - Marilyn (WPSolving Volunteer)