2

Looking at the find command I am curious what the difference is between the two:

find -type f

and

find ${1} -type f

They both seem to perform the same function so what is the ${1} for?

  • what is the reason to use ${1} in find? In arbitrary case ${1} gives you nothing – RomanPerekhrest Aug 30 '17 at 16:59
  • 2
    BSD Linux requires the directory you want to find files in. GNU Linux assumes no current dir means you want to find in the current working directory (plus subdirs) – Sridhar Sarnobat Aug 30 '17 at 17:55
2

This ${1} has nothing to do with the find, it is a shell parameter.

If the second find command runs inside a shell script, for example is your test.sh is like:

#!/bin/bash
find ${1} -type f

Then, if you call this test.sh with

./test.sh cica

Then the cica will be substituted by the shell into the find command line. The find command will see a

find cica -type f

..and so will it run (thus, it will look for files in the cica directory and not in the current directory).

P.s. Once in our lifes we all should read the manual of bash, ls, cp and strace.

| improve this answer | |
  • and find, and sed, and grep, and... – DopeGhoti Aug 30 '17 at 17:06
  • @DopeGhoti Right, I didn't want to overload him on the spot, but I agree :-) – peterh - Reinstate Monica Aug 30 '17 at 17:07
2

As @DopeGhoti mentioned, ${1} (or $1) is the first argument to a script or function.

I suspect that the code you're questioning is a part of a function that looks something like this:

#/bin/bash
function show {
    find ${1} -type f -print
}
show        #...find with current directory as starting point...
show mydir  #...find using `mydir` as starting point...

That is, if no argument is present when the function is called, ${1} is empty and find -type f... is run.

| improve this answer | |
1

${1} (or $1) is the first argument to a script or function. The command you call out in your question is likely in a script. For a very basic example:

#!/bin/bash
find "${1}" -type f

If that file were saved as an executable seek.sh, and you ran the command ./seek.sh /home, the command that would be executed is find "/home" -type f.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.