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

  • Home
  • SEARCH
  • 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 8379851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:18:02+00:00 2026-06-09T16:18:02+00:00

How to pass a regular expression as a parameter to a shell script? I

  • 0

How to pass a regular expression as a parameter to a shell script?

I need to write a shell script, which will take parameters and give them to unix commands. And I’d like to use regular expressions there. Is this possible at all?

Or reformulation – how to write the script equivalent to “cp” command, using only this command?

I’m trying to make a file “mycp”

#!/bin/bash -fx
cp $2 $1 

and call it by

mycp myDir "*sh" 

and want it to do the same as “cp *sh myDir”.
But resulting bash interpretation is:

+ cp '*sh' myDir
cp: *sh: No such file or directory
  • 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-09T16:18:04+00:00Added an answer on June 9, 2026 at 4:18 pm

    Revised question

    I’m trying to make a file "mycp"

    #!/bin/bash -fx
    cp $2 $1 
    

    and call it by

    mycp myDir "*sh" 
    

    In that case, you still need eval, but you’d write:

    #!/bin/bash -x
    eval cp "$2" "$1"
    

    You’re running into problems because you have specified the -f option. man bash says (in part):

    After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [.

    Remove the f from the ‘shebang’ (first) line of the script.


    Original question

    Given that you want mycp "*sh*" aa to do shell expansion on the argument, you’ll probably end up using eval in your script:

    eval cp "$@"
    

    However, the use of eval is dangerous; it can lead to unexpected side-effects. The use of "$@" is important; it preserves the number of arguments and spaces in them. Unfortunately, using eval then undoes that, but we can’t have everything — or not easily.


    For quite a long time (say 1987* to 1999), I used this script as a cover for cp:

    :   "@(#)$Id: cp.sh,v 1.3 1997/06/02 21:45:00 johnl Exp $"
    #
    #   Alternative copy command
    
    case $# in
    0)  /bin/cp     ;;
    1)  /bin/cp $1 .    ;;
    2)  /bin/cp "$@"    ;;
    *)  if [ -d `la "$@"` ]
        then    /bin/cp "$@"
        else    /bin/cp "$@" .
        fi;;
    esac
    

    It uses a very simple C program called la (for ‘last argument’) to get the last argument and checks whether the given last argument is a directory. More than 99% of the time, if I typed (by accident) ‘cp /some/where/sh‘ rather than cp /some/where/*sh* ., the second was what I meant, and the script fixed things. I haven’t used it for quite some time; it may have been in the last millennium, but was probably sometime earlier in this one that I gave up using it.

    * Although the version string says ‘1997’, the code is identical to the 1987 version. Version 1.1 and 1.2 were under SCCS and used different SCCS ID strings; the conversion to RCS made them identical. Version 1.3 reinstated the @(#) identifier string used by the SCCS what command to the RCS version handling. So, the script is ancient — 1987, really.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to write a regular expression that will pass a numbers from 60 to
The regular expression ^/wp-[^./]+\.php$ matches: wp-config.php wp-commentsrss2.php wp-pass.php wp-rdf.php [...] But I need another
I am try to write a regular expression in javascript that will not allow
I am trying to write a regular expression but I can't pass the words
Is that possible to pass variable into regular expression pattern string in jquery (
Possible Duplicate: How do you pass a variable to a Regular Expression JavaScript? This
I am trying to write a regular expression string match in vb.net. The condition
I'm used to regular expression syntax which allows for named matches from the search
I have the following regular expression, which lets me parse percentages like '20%+', '20%',
Is it possible to pass regular expression to the list of allowed html attributes

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.