Search code examples
bashpositional-parameter

trouble understanding difference in 'find' output for bash using prompt, positional parameters


So I am trying to understand the difference between two results in Bash to further my understanding of positional parameters:

$ find ./*.md
one.md
two.md
three.md
four.md

and:

$ ./findall.sh ./*.md
one.md

where findall.sh is:

#!/usr/bin/env bash
find $1 

In my understanding, these two should operations should be identical, but the use of positional parameters seems to return only one item. What am I not getting?


Solution

  • In both cases, your interactive bash is expanding ./*.md before calling find. So your first command expands to this:

    find ./one.md ./two.md ./three.md ./four.md
    

    In the second case, your command expands to this:

    ./findall.sh ./one.md ./two.md ./three.md ./four.md
    

    Then the script runs, and expands the command in the script to this:

    find ./one.md
    

    Perhaps you meant to quote the wildcard:

    find './*.md'
    ./findall.sh './*.md'
    

    But in either case, find will fail because the first arguments to find (before any arguments that start with -) are the names of directories in which to search. There is no directory whose name is ./*.md, because / cannot occur in a file or directory name.

    Perhaps you meant this, to find all files whose names match *.md, anywhere under the current directory:

    find . -name '*.md'
    

    Perhaps you meant this, to find all files in the current directory (but not subdirectories) whose names match *.md:

    find . -maxdepth 1 -name '*.md'