0

This may end up being a duplicate, but it proved quite hard to search for.

My problem is the following. Imagine a script that takes two parameters, say prog and args and passes args to prog. I want the quoting of args to be preserved for prog.

Let's take these two scripts as an example:

$ cat x
#! /bin/bash

while [ $# -gt 0 ]; do
    echo "$1"
    shift
done

$ cat y
#! /bin/bash

echo $1
./x $1

The first script (x) just prints the arguments in a line (so to tell how the arguments broke down). The second script passes the arguments to the first script.

Here's what happens:

$ ./y 'a b "x y"'
a b "x y"
a
b
"x
y"

As the first line (from echo) shows, $1 correctly expands to a b "x y" with the quotes around "x y" in place. So why, when passed to another script ./x $1, the "x y" term is not kept as a single argument, but instead as two ("x and y")?

How can I prevent this? I.e., how can I pass the set of arguments given to y as a single argument (i.e., 'a b "x y"') to x as multiple arguments with the quoting taking effect?


P.S. I had also tried with $@ with no difference in behavior. %q of printf didn't make anything sensible either. Just to be clear, my expected output is:

$ ./y 'a b "x y"'
a b "x y"
a
b
x y

Edit: What I'm trying to do is separate arguments given to two programs in the y script. For example:

$ ./y 'args to prog1' 'args to prog2'

and have y run prog1 with its own arguments and prog2 with its own.

2 Answers 2

2

A workaround I found was to not give arguments as a single argument (like 'separate arguments in one '), but give them separately, in the script find and store them in array and then pass them to the programs. To be able to distinguish between parameters of the first and the second program, I used a separator (in this case ::).

For example:

$ cat y
#! /bin/bash

i=0
while [ $# -gt 0 ] && [ "$1" != "::" ]; do
    args1[i]="$1"
    ((i=i+1))
    shift
done

has_second=false
if [ "$1" == "::" ]; then
    shift
    has_second=true
fi

if $has_second; then
    i=0
    while [ $# -gt 0 ]; do
        args2[i]="$1"
        ((i=i+1))
        shift
    done
fi

echo "First:"
./x "${args1[@]}"
echo "Second:"
./x "${args2[@]}"
2
  • Good idea; I would suggest using a different separator than --, as that is commonly used to indicate that all following arguments should be treated normally, even if they match an option recognized by the programs. Something like :-: might work nicely.
    – chepner
    Dec 3, 2013 at 14:52
  • @chepner, you are right. I would change -- to something else.
    – Shahbaz
    Dec 3, 2013 at 14:56
0

Use quotes in script y:

#!/bin/bash

echo "$1"
./x "$1"

Without quotes around $1 string $1 is passed as multiple arguments to script x.

3
  • 1
    Then $1 would be sent to x as a single argument instead of many. See my edit on expected output.
    – Shahbaz
    Dec 3, 2013 at 11:42
  • Yes that's correct $1 will be sent as single argument using quotes.
    – anubhava
    Dec 3, 2013 at 11:43
  • 1
    If you place echo $# on top of script x you will notice 4 instead of 1 if you call ./x $1 instead of ./x "$1"
    – anubhava
    Dec 3, 2013 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.