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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:21:14+00:00 2026-06-14T04:21:14+00:00

I want to find whether a string contains a forward slash using grep command.

  • 0

I want to find whether a string contains a forward slash using “grep” command. It is easy at the beginning, and I wrote the following script.

foo=someone/books
if [ `echo "$foo" | grep '/'` ]
then
    echo "has forward slash"
fi

however, the problem is in the corner, if I set the variable “foo” to the following string,

foo="someone/books in stack"

The above script will be failed since there is “space” in variable foo, when the command expands, the condition in if statement is as below.

grep '/' someone/books in stack

Because of “space”, the above “grep” command has too many arguments that is illegal. FYI, I try to solved this problem using case statement:

case $foo in
    */*)
        echo "has forward slash"
        ;;
    *)
        ;;
esac

However, I do not want to use case statement since it’s verbose. So how could I solved this problem using grep command or others?

  • 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-14T04:21:15+00:00Added an answer on June 14, 2026 at 4:21 am

    You are not using the if-statement correctly. The command in the if-condition needs to be quoted so that it becomes a single string when expanded. Like this:

    if [ "`echo "$foo" | grep '/'`" ]
    then
        echo "has forward slash"
    fi
    

    Or even better is if you check the return code of grep in your if-condition:

    if $(echo "$foo" | grep -q '/')
    then
        echo "has forward slash"
    fi
    

    You can also do away with the grep and use this instead:

    foo="someone/books in stack"
    if [[ "$foo" == */* ]]
    then
      echo "has forward slash"
    fi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find whether a string contains any of the special characters like
I want to find particular string in JAVA and I am using CONTAINS function
I want to check whether string contains particular sub string and using CONTAINS() for
How to find whether a string array contains some part of string? I have
I want to check whether a string contains # or not. Then if it
Below is my code in which i want to find whether string contain $
Possible Duplicate: How to find whether a string contains any of the special characters?
String link = http://hosted.ap.org; I want to find whether the given url is already
I've got a string containing an SQL statement. I want to find out whether
I want to find out whether a certificate's private key is stored in a

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.