Skip to main content
Fix word in title, tags
Link
Kusalananda
  • 357.6k
  • 42
  • 743
  • 1.1k

Find exec sh: EnvironmentShell variable not getting passed to subshell

Source Link
Porcupine
  • 2.2k
  • 4
  • 26
  • 60

Find exec sh: Environment variable not getting passed to subshell

Here is a simplified code that prints the name of Directory if it contains a Filename with same name as the parent directory and .md extension.

FIND(){
    find . -type d -exec sh -c '
        for d do
            [ -f "${d}/${d##*/}.md" ] && printf "%s\n" "$d"
        done' find-sh {} +
}


FIND

To generalize I want to send the Search term ${d}/${d##*/}.md as an argument to the FIND function, but unfortunately this does not outputs anything:

FIND(){
    local SearchTerm="${1}"
    find . -type d -exec sh -c '
        for d do
            [ -f "${SearchTerm}" ] && printf "%s\n" "$d"
        done' find-sh {} +
}

FIND '${d}/${d##*/}.md'

I am sure there is some issue with the quotation of the SearchTerm. Any hints?


I tried: FIND '\${d}/\${d##*/}.md' but has no output