Start New Discussion within our Software Development Community

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:

#!/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. Thanks and regards!

More info:
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

#!/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}

Thanks for your help!

This article has been dead for over six months. Start a new discussion instead.