I have an android build i need to customize, the question is ehre and not in stackoverflow because my issue wis with bash. I wrote a script:

#!/bin/bash -xv
if [ "$#" -ge "2" ]; then
    params="$params --custom-package com.some.package"
    while [ "$#" -gt "0" ]; do
        echo $1 | grep ' ' > /dev/null
        if [ "$?" -eq "0" ]; then
        params="$params $temp"

path=$(echo $0|xargs dirname)
echo $params
$path/aapt-orig $params

the script suppose to take the parameters it got, add --custom-package com.some.package parameter and keep the rest of the params, the first ($1) param is the action so i need to keep it first.

The problem is with spaces in directory names './my android dir/' for example. if i just pass the parameter through: param ="$param $1"; shit; if i have spaces in the name it doesn't work cause the new param is not escaped(with "), if i use the code above it still doesn't work because for some reason bash ignores the " char and still consider the space in the middle as a separate parameter! here is the output from the run: './aapt action "/Users/bankleumi/some dir/" another_param'

'[' 0 -eq 0 ']'
+ temp='"/Users/bankleumi/some dir/"'
+ params='action --custom-package com.leumi.leumiwallet "/Users/bankleumi/some dir/"'
+ shift
+ '[' 1 -gt 0 ']'
+ temp=another_param
+ echo another_param
+ grep ' '
+ '[' 1 -eq 0 ']'
+ params='action --custom-package com.leumi.leumiwallet "/Users/bankleumi/some dir/" another_param'
+ shift
+ '[' 0 -gt 0 ']'
path=$(echo $0|xargs dirname)
echo $0|xargs dirname
++ echo ./aapt
++ xargs dirname
+ path=.
echo $params
+ echo action --custom-package com.leumi.leumiwallet '"/Users/bankleumi/some' 'dir/"' another_param
action --custom-package com.leumi.leumiwallet "/Users/bankleumi/some dir/" another_param
$path/aapt-orig $params
+ ./aapt-orig action --custom-package com.leumi.leumiwallet '"/Users/bankleumi/some' 'dir/"' another_param

As you can see in the end in the printing of the echo bash consider the string "/Users/bankleumi/some dir" as two strings even though there are \" integrated IN the string! how can i make bash leave the space alone? ( i tried playing with IFS and it didn't work either...)

2 Answers 2


Don't mess with re-quoting, it won't work due to the way sh/bash use word-splitting on variables -- that is, they split the value without first considering the quotes in the value.

You could get around that by using eval "$path/aapt-orig $params"...

But Bash has arrays, use them instead:

#!/usr/bin/env bash
args+=("$1"); shift
args+=(--custom-package com.leumi.leumiwallet)
for arg in "$@"; do
exec "$(dirname "$0")/aapt-orig" "${args[@]}"

Or even simpler:

#!/usr/bin/env bash
exec "${0%/*}/aapt-orig" "$1" --custom-package com.leumi.leumiwallet "${@:2}"
  • 2
    Side note: echo $0 | xargs dirname? Really? Oct 4, 2012 at 13:08
  • For some reason which I am sure @grawity will understand better than I, using double quotes also works: ./aapt action ""/Users/bankleumi/some dir/"" another_param
    – terdon
    Oct 4, 2012 at 13:23
  • @terdon: I'm not sure how that would work. After bash's unquoting, ""foo bar"" baz is exactly the same as foo bar baz, and aapt receives it as three single-word arguments. Oct 4, 2012 at 13:38
  • No idea, @grawity, just tried it on a whim and it worked. I used the script in the OP and just double quoted the CLI argument as shown above. The relevant output was: + echo action --custom-package com.some.package /Users/bankleumi/some dir/ another_param. Bash version: GNU bash, version 4.2.36(1)-release (x86_64-pc-linux-gnu)
    – terdon
    Oct 4, 2012 at 13:41

This should work with more shells, and may be easier to understand:

first="$1"; shift;
exec "`dirname '$0'`/aapt-orig" "$first" --custom-package com.foo.bar "$@";

Note that it works because shells treat "$@" specially, see section "Special Parameters" in the Bash manpage.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .