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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 "$@"

  2. #2
    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