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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:34:46+00:00 2026-06-01T20:34:46+00:00

$ sh sh-3.2$ if > ps -ef | grep apple ; > then >

  • 0
$ sh
sh-3.2$ if
> ps -ef | grep apple ;
> then
> echo APPLE 
> fi ;
lazer   7584  7571  0 04:36 pts/4    00:00:00 grep apple
APPLE
sh-3.2$ exit
exit
$ which sh
/bin/sh
$ /bin/sh -c if ps -ef | grep apple ; then echo APPLE fi ;
bash: syntax error near unexpected token `then'
$

As above, my simple if statement works as expected when executed line by line but gives me the following error when executed using sh -c:

bash: syntax error near unexpected token `then’

What am I missing 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-01T20:34:48+00:00Added an answer on June 1, 2026 at 8:34 pm

    Your interactive shell will be escaping the invocation via sh -c. In particular it’s taking everyting after the semi-colon as a new statement.

    Quote everything that you’re feeding to /bin/sh e.g.

    $ /bin/sh -c "if ps -ef | grep apple ; then echo APPLE fi ;"
    

    I think you may also need to delimit further using semi-colons given that you’re condensing everything onto one line, and would perhaps suggest you could use a heredoc.

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

Sidebar

Related Questions

GNU grep 2.5.4 on bash 4.1.5(1) on Ubuntu 10.04 This matches $ echo this
Recently I came across following grep command: /usr/xpg4/bin/grep -Ff grep.txt input.txt > output.txt which
If I run grep -i echo * I get the results I want, but
I have a script which uses grep to output like this: 12 Apples 10
grep has a -w option which instructs it to match on a whole word
grep -R \\/web * gives me some occurrences which i want to change to
grep fails when using both --ignore-case and --only-match options. Example: $ echo abc |
git grep fizzbuzz $(git rev-list --all) fatal: Invalid object name 'Symbol's function definition is
I'm writing a grep function in C++ (as a self assigned exercise - I
I want to grep my server logs to collect all of the unique IP

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.