Thread: argument with space passed to bash script and further to executable

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    argument with space passed to bash script and further to executable

    Hi,
    I'd like to pass arguments with space inside to a bash script and further into an executable called inside the script. My bash script looks like:
    Code:
    #!/bin/bash
    ARG_OPTS=""
    while [[ -n "$1" ]];
            ARG_OPTS="${ARG_OPTS} $1"
            shift
    done
    my_executable ${ARG_OPTS}
    one of the arguments to the executable is like
    --options='-t 0 -v 0'
    To prevent it from being splitted by the spaces when first passing to the bash script, i.e. "--options='-t", "0", "-v" and "0", I double quote it as
    "--options='-t 0 -v 0'"
    So '-t 0 -v 0' as a whole will be able to preserved until reaching the command invoking the executable. However '-t 0 -v 0' is not passed as a whole into the executable, but split by spaces again. This may sounds like it is the problem of calling the executable, however when directly invoking the executable from terminal
    my_executable --options='-t 0 -v 0'
    could be passed successfully as argument. So can
    my_executable --options="-t 0 -v 0"
    .

    Hope that I could explain my question clearly. Shall I change the argument format or the bash script. Really appreciate your advice!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    For example look at how gcc can pass arguments through to the linker. Since these arguments can contain a space, some extra processing has to be done because shell variables don't really have the ability to act the way you want.

    You could replace the argument separator character (space) with some other character. So you might pass this to your executable:

    "--options=-t,0,-v,0"

    It would then turn the commas back into spaces.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Quote Originally Posted by lehe View Post
    Hi,
    I'd like to pass arguments with space inside to a bash script and further into an executable called inside the script. My bash script looks like:
    Code:
    #!/bin/bash
    ARG_OPTS=""
    while [[ -n "$1" ]];
            ARG_OPTS="${ARG_OPTS} $1"
            shift
    done
    my_executable ${ARG_OPTS}
    one of the arguments to the executable is like

    To prevent it from being splitted by the spaces when first passing to the bash script, i.e. "--options='-t", "0", "-v" and "0", I double quote it as


    So '-t 0 -v 0' as a whole will be able to preserved until reaching the command invoking the executable. However '-t 0 -v 0' is not passed as a whole into the executable, but split by spaces again. This may sounds like it is the problem of calling the executable, however when directly invoking the executable from terminal could be passed successfully as argument. So can .

    Hope that I could explain my question clearly. Shall I change the argument format or the bash script. Really appreciate your advice!

    Use "$@"
    Code:
    #!/bin/sh
    
    my_executable "$@"

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thank you!

    So if I understand correctly, brewbuck suggested to modify the source code of the executable to parse its arguments with nonspace charater as the separator instead? Is there a way without modifying the source code of the executable?

    The reason why I don't use "$@" is that the arguments to the bash script is not completely those for the executable. Some of them are just arguments only to the bash script. So actually the script is like
    Code:
    #!/bin/bash
    DEBUGGER=""
    ARG_OPTS=""
    while [[ -n "$1" ]];
            case $1 in
                --run)
    		make clean
                    make -j -k || exit 1
    		DEBUGGER=""
                    ;;
                --gdb)
    		make clean
    		make -j -k DEBUG=yes || exit 1
                    DEBUGGER="gdb --args"
                    ;;
                *)
                    ARG_OPTS="${ARG_OPTS} $1"  
                    ;;
            esac
    	shift
    done
    ${DEBUGGER} my_executable ${ARG_OPTS}

Popular pages Recent additions subscribe to a feed