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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:14:29+00:00 2026-05-22T15:14:29+00:00

I have bash script that takes in a couple of arguments tests then and

  • 0

I have bash script that takes in a couple of arguments tests then and runs a command as shown below

call script => /bin/bash myscript arg1 arg2 arg3 arg4
comm called => command -a arg1 -b arg2 -c arg3 -d arg4

if a parameter is empty then that option is not called.

call script => /bin/bash myscript arg1 arg2 '' arg4
comm called => command -a arg1 -b arg2 -d arg4

I am able to achieve this by using following line in the script to test the arguments

if test "${arg3:-t}" != "t" ; then

This script works like a charm when called from prompt. even if I replace ” with “” for an empty argument it works fine.

This starts failing when I call this script from java using exec.

Process p = Runtime.getRuntime().exec("/bin/bash myscript arg1 arg2 '' arg4");
expected command => command -a arg1 -b arg2 -d arg4 (as in above example)
actual command   => command -a arg1 -b arg2 -c '' -d arg4

I am not able to understand why this would be happening. Where is the problem? In the shell script or in the way it command is executed from java?

How can this be fixed?

  • 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-22T15:14:30+00:00Added an answer on May 22, 2026 at 3:14 pm

    The basic problem is that java doesn’t do anything at all fancy to parse the string you give it and break it nicely into arguments.

    The short answer is to use the String [] version of Runtime.exec:

    Runtime.getRuntime().exec(
        new String[] {"/bin/bash", "myscript", "arg1", "arg2", "",  "arg4"});
    

    If you have other places where the argument parsing is more convoluted than that, you can pass the parsing of the string off to bash, and do:

    Runtime.getRuntime().exec(
        new String[] {"/bin/bash", "-c", "/bin/bash myscript arg1 arg2 '' arg4"});
    

    If you’re converting something over that’s doing a huge number of complicated redirects like 2>&1 or setting up a whole pipeline, you might need this bash -c trick.

    EDIT:

    To understand what’s going on here, you have to realize that when user-space code tells the kernel “load this executable with these arguments and start a process based on that” (*), what’s passed on to the kernel is an executable, an array of strings for arguments (the 0th/first element of this argument array is the name of the executable, except when doing something weird), and an array of strings for the environment.

    What bash does when it sees this line:

    /bin/bash myscript arg1 arg2 '' arg4
    

    is think “Okay, /bin/bash isn’t a builtin, so I’m executing something. Let’s put together the argument array for the subprocess using my string parsing algorithms, which know about quotes and whatnot”. Bash then determines that the arguments to pass the kernel for the new process are:

    • /bin/bash

    • myscript

    • arg1

    • arg2

    • (empty string)

    • arg4

    Now bash has pretty complicated string processing algorithms that it applies here – it accepts two different kinds of quotes, it’ll do expansion of $VAR when it happens outside strings or inside double quotes, it’ll replace subcommands in backquotes with the output, etc.

    Java doesn’t do anything so sophisticated when you call the single string version of exec. It just creates a new StringTokenizer and uses that to break up the string you give it into arguments. That class doesn’t know anything about quotes; it splits that string up into:

    • /bin/bash

    • myscript

    • arg1

    • arg2

    • '' (a string with two characters, both of which are the single quote)

    • arg4

    Java then calls the String[] version of exec. (Well, one of them)

    Notes for people picking nits with this:

    (*) Yes, I’m deliberately eliding the difference between the system calls fork and execve. Pretend they’re one call for the moment.

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

Sidebar

Related Questions

Alright, I have a script that takes a few arguments, runs data, and then
I have a bash script that runs a simulation program written in Fortran 90,
I have a complex command that I'd like to make a shell/bash script of.
I have a script that takes a command and executes it on a remote
I have a simple bash script, called print_code.sh , that takes a file name,
He all, i have a problem with my bash script. That's my code: #!/bin/bash
I have a bash script that takes the date, month and year as separate
I have a bash script that tests whether the sftp connection exists, very simple
I have a bash script that creates a Subversion patch file for the current
I have a bash shell script that loops through all child directories (but not

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.