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 7757975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:14:11+00:00 2026-06-01T13:14:11+00:00

I’m a newbie in bash scripts and have the following problem : I’m trying

  • 0

I’m a newbie in bash scripts and have the following problem :
I’m trying to make a search script that let me search for files containing a certain string with find and / or grep and optional parameters, and I’m stuck with one last problem, the * character that doesn’t match the condition in all cases.

There are 3 parameters to my script :

  • string_to_search (required)
  • file_name_pattern (optional)
  • path (optional)

Example :

./search_text.sh 'string_to_search' 'file_name_pattern' /path

I want to get the following :

  • If file_name_pattern is not specified or is * (with or without quotes) => grep
  • If file_name_pattern is anything else than * or ‘*’ => find + grep

Here’s my code :

#!/bin/bash

# ------------------------------------
# Set default values for optional parameters (tests)

if [ ! -z $2 ] 
then 
    FILE_TYPE=$2
    echo 'FILE_TYPE : var 2'
else
    FILE_TYPE="*"
    echo 'FILE_TYPE : *'
fi

if [ ! -z $3 ] 
then 
    LOCATION=$3
    echo 'LOCATION : var 3'
else
    LOCATION=.
    echo 'LOCATION : .'
fi

# ------------------------------------
# Do search / show conditions

if [ "$FILE_TYPE" == \* ];
  then 
    #grep -rn -C 0  --color=always  "$1" $LOCATION | less -R;
    echo ' => GREP';
  else
    #find $LOCATION -iname $FILE_TYPE | sort | xargs grep "$1" --color=always -n  2>/dev/null | less -R;
    echo '=> FIND';
fi

Just run this code to show the 2 echos at the end for testing.

Now my problem is :

./search_text.sh 'string_to_search' '*.php' /path  => works (FIND)
./search_text.sh 'string_to_search' '*' /path      => works (GREP)
./search_text.sh 'string_to_search' '*'            => works (GREP)
./search_text.sh 'string_to_search' *              => ERROR => goes to FIND INSTEAD OF GREP :-(
./search_text.sh 'string_to_search'                => works (GREP)

In other words how could I write the following condition :

   if [ "$FILE_TYPE" == \* ];

so that * should be considered the same as ‘*’ when passing these values as a parameter in command line ?

Even escaping the * with \* the condition isn’t matched when we only pass * without quotes to the script.

In other words, I’d like to make the * to be considered as a normal wildcard like in any other command, so that * = ‘*’

Many thanks for any help !

  • 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-01T13:14:12+00:00Added an answer on June 1, 2026 at 1:14 pm

    You can disable all glob expansion using

     set -o noglob
    

    Afterwards * is not special:

    $ ls *
    ls: *: No such file or directory
    

    However, there is no way to force your users to do this before calling your script. Since the user’s shell, not your script, controls globbing you are left with no recourse.

    Your only option would be to treat

    ./search_text.sh 'string_to_search' *
    

    Exactly like

    ./search_text.sh 'string_to_search' file1 file2
    

    Since that’s what it is anyway.

    In other words, when there is no trailing directory specified and $1 is not a literal * use the grep case.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.