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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:00:50+00:00 2026-05-28T02:00:50+00:00

I want to write a Unix shell script that will do various logic if

  • 0

I want to write a Unix shell script that will do various logic if there is a string inside of another string. For example, if I am in a certain folder, branch off. Could someone please tell me how to accomplish this? If possible I would like to make this not shell specific (i.e. not bash only) but if there’s no other way I can make do with that.

#!/usr/bin/env sh

if [ "$PWD" contains "String1" ]
then
    echo "String1 present"
elif [ "$PWD" contains "String2" ]
then
    echo "String2 present"
else
    echo "Else"
fi
  • 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-05-28T02:00:50+00:00Added an answer on May 28, 2026 at 2:00 am

    Here’s yet another solution. This uses POSIX substring parameter expansion, so it works in Bash, Dash, KornShell (ksh), Z shell (zsh), etc. It also supports special characters in strings.

    test "${string#*"$word"}" != "$string" && echo "$word found in $string"
    

    A functionalized version with some tests:

    # contains(string, substring)
    #
    # Returns 0 if the specified string contains the specified substring,
    # otherwise returns 1.
    contains() {
        string="$1"
        substring="$2"
        if [ "${string#*"$substring"}" != "$string" ]; then
            return 0    # $substring is in $string
        else
            return 1    # $substring is not in $string
        fi
    }
    
    testcontains() {
        testnum="$1"
        expected="$2"
        string="$3"
        substring="$4"
        contains "$string" "$substring"
        result=$?
        if [ $result -eq $expected ]; then
            echo "test $testnum passed"
        else
            echo "test $testnum FAILED: string=<$string> substring=<$substring> result=<$result> expected=<$expected>"
        fi
    }
    
    testcontains  1 1 'abcd' 'e'
    testcontains  2 0 'abcd' 'ab'
    testcontains  3 0 'abcd' 'bc'
    testcontains  4 0 'abcd' 'cd'
    testcontains  5 0 'abcd' 'abcd'
    testcontains  6 1 '' 'a'
    testcontains  7 0 'abcd efgh' 'cd ef'
    testcontains  8 0 'abcd efgh' ' '
    testcontains  9 1 'abcdefgh' ' '
    testcontains 10 0 'abcd [efg] hij' '[efg]'
    testcontains 11 1 'abcd [efg] hij' '[effg]'
    testcontains 12 0 'abcd *efg* hij' '*efg*'
    testcontains 13 0 'abcd *efg* hij' 'd *efg* h'
    testcontains 14 1 'abcd *efg* hij' '*effg*'
    testcontains 15 1 'abcd *efg* hij' '\effg\'
    testcontains 16 0 'a\b' '\'
    testcontains 17 0 '\' '\'
    testcontains 18 1 '[' '\'
    testcontains 19 1 '\' '['
    testcontains 20 0 '-n' 'n'
    testcontains 21 1 'n' '-n'
    testcontains 22 0 '*\`[]' '\`'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am working on unix. i want to write a shell script which will
I want to write a script that fills a directory with music until there's
I want to write a unix/linux program, that will use a configuration file. My
I want to write some JavaScript that will change the onmousedown of a div
Say, I want to write a desktop application that will heavily use SVG. What
I need to write a shell script that pick all the files (not directories)
I want to write a program in C++ that should work on Unix and
I want to write a command that specifies the word under the cursor in
I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an

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.