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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:57:57+00:00 2026-06-15T03:57:57+00:00

I want to run the command mvn clean in a bash script. But I

  • 0

I want to run the command mvn clean in a bash script. But I want to put it in an if statement. If the clean does not run properly I would like to exit out of the bash script with an echo statement.
Here is the code that is causing the problem:
if [ mvn clean ]; then

I tried putting $(mvn clean) inside the if statement but there were too many arguments says the terminal. Does anyone know if this is possible? Thanks!

  • 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-15T03:57:59+00:00Added an answer on June 15, 2026 at 3:57 am

    Here’s what you want:

    mvn clean
    if [ "$?" -ne 0 ]; then
        echo "Maven Clean Unsuccessful!"
        exit 1
    fi
    

    Explanation:

    • $? is a special shell variable that contains the exit code (whether it terminated successfully, or not) of the most immediate recently executed command.
    • -ne is an option to the test builtin [. It stands for “not equal”. So here we are testing if the exit code from mvn clean is not equal to zero.
    • echo "Maven Clean Unsucccessful!" – If this is the case, then we output some indicative message, and exit the script itself with an errant exit code.

    When you do $(mvn clean), that instead spawns a new subshell to run mvn clean, then simply dumps everything that was output to stdout in that subshell from running mvn clean to where $(...) was used in the parent shell.


    Alternatively, you can do:

    mvn clean || { echo "Maven Clean Unsuccessful"; exit 1; }
    

    Which is just shorthand syntactic sugar for doing the same thing.

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

Sidebar

Related Questions

I want to run mvn tomcat:run from the command line, but how can I
tmp-file contains: database_1 database_2 database_3 I want to run a command like mysqldump DATABASE
I want to run a bash command once for each line of a text
I want to create a shell script which when called run the following command
I want to run command like this: vim -c %g/blablabla/norm /str<ESC>cwSTR file How I
I want to run a command line program from within a python script and
I want to do something between instrumentation and test when I run command mvn
I want to run the command: nc localhost 9998 Then I want my script
I want to run some command in python script import fcntl KDSETLED = 0x4B32
I want to be able to run the following command using maven mvn tomcat7:deploy

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.