I would like to run recursively myscript.sh, to execute all files in the directory:
It has been discussed here that I could do like this:
#!/bin/bash
for file in * ; do
echo $file
done
But I would like myscript.sh to execute with this syntax, so that I could select only certain filetypes to be executed:
./myscript.sh *.dat
Thus I modify the script above:
#!/bin/bash
for file in $1 ; do
echo $file
done
In which when executing, it only executes first occurrence, not all files with *.dat extensions.
What is wrong here?
The wildcard
*.datis expanded by the shell before your script ever sees it. So the filenames show up in your script as$1,$2,$3, etc.You can work with them all at once by using the special
$@variable:Note that the double quotes around
"$@"is special. Fromman bash: