How to write a bash function that invokes a grep command with constructed fileName parameter

bash, function, grep, parameters, unix

Solution

This happens because Variable referencing is disabled by single quotes, which cause the "$" to be interpreted literally.

For example if you do:

var="test"
echo "$var"    # will print test
echo '$var'    # will print $var

For more info, read this: Variable Substitution, Advanced Bash-Scripting Guide.

Problem

I would like to create a bash function that calls the well known `grep` command. This function should take the grep pattern as a parameter. Now comes the tricky part. The function itself constructs the fileName parameter given to the `grep` command. Upon calling the function it seems that the `grep` command is being called but there is absolutely no output to the console. Here is what I have come up with so far: ``` specialGrep() { evaluatedFileNamePart=$(someOtherGrepCommandThatEvaluatesAnotherPartOfThePath) # take function param as pattern and use constructed filename param grep -i '${1}' /fixedPathPart1/"$evaluatedFileNamePart"/fixedPathPart2/*.* } ``` What's wrong here?

Original source