Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8239977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:19:51+00:00 2026-06-07T20:19:51+00:00

I would like to run recursively myscript.sh, to execute all files in the directory:

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:19:53+00:00Added an answer on June 7, 2026 at 8:19 pm

    The wildcard *.dat is 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:

    for file in "$@"; do
        echo $file
    done
    

    Note that the double quotes around "$@" is special. From man bash:

    @  Expands to the positional parameters, starting from one.  When  the  expansion
       occurs  within double quotes, each parameter expands to a separate word.  That
       is, "$@" is equivalent to "$1" "$2" ...  If the double-quoted expansion occurs
       within  a word, the expansion of the first parameter is joined with the begin-
       ning part of the original word, and the expansion of  the  last  parameter  is
       joined  with the last part of the original word.  When there are no positional
       parameters, "$@" and $@ expand to nothing (i.e., they are removed).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a bash script that recursively cleans up every filename
I would like to run this script (embed drive list in a site) that
I would like to run APIDemos under platforms\android-1.5\samples directory on Android emulator but I
I would like to run a (extensive) query that produces one line of XML.
I have found a useful shell script that shows all files in a directory
I am writing a java program that runs under unix. It would like run
I would like to run an executable that performs some processing on a dataset
I would like to list the versioned files in the root directory of a
I would like to run a job through cron that will be executed every
I've got some heavyweight process, which does some video processing. I would like run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.