From: Tobias Nissen on
Hi!

I have a suite of programs, say kd-list, kd-process, kd-process-file
and kd-unmark. I want to create a wrapper script, kd, allowing me to
call the scripts like this: `kd list arg1 arg2 ...`. Many programs
have an interface similar to this, git e.g.

In kd I'm currently doing sth like this:

command="$1"
shift
args="$@"
...
kd-$command $args

Now this works for simple arguments. But when it comes to quotes it
brakes, since it doesn't retain the quotes. I.e., if I execute
kd list -w "foo bar"
I want it to be translated into a call of kd-list like this:
kd-list -w "foo bar"
but with the approach from above I get
kd-list -w foo bar
and kd-list sees only "foo" as a -w's parameter, not "foo bar".

How can I retain the quotes?

Thanks in advance!
Tobias
From: Janis on
On 24 Feb., 15:30, Tobias Nissen <t...(a)movb.de> wrote:
> Hi!
>
> I have a suite of programs, say kd-list, kd-process, kd-process-file
> and kd-unmark. I want to create a wrapper script, kd, allowing me to
> call the scripts like this: `kd list arg1 arg2 ...`. Many programs
> have an interface similar to this, git e.g.
>
> In kd I'm currently doing sth like this:
>
>   command="$1"
>   shift
>   args="$@"
>   ...
>   kd-$command $args
>
> Now this works for simple arguments. But when it comes to quotes it
> brakes, since it doesn't retain the quotes. I.e., if I execute
>   kd list -w "foo bar"
> I want it to be translated into a call of kd-list like this:
>   kd-list -w "foo bar"
> but with the approach from above I get
>   kd-list -w foo bar
> and kd-list sees only "foo" as a -w's parameter, not "foo bar".
>
> How can I retain the quotes?

Use double quotes around variable expansions.

Janis

>
> Thanks in advance!
> Tobias

From: Tobias Nissen on
Janis wrote:
> On 24 Feb., 15:30, Tobias Nissen <t...(a)movb.de> wrote:
[...]
>>   command="$1"
>>   shift
>>   args="$@"
>>   ...
>>   kd-$command $args
>>
>> Now this works for simple arguments. But when it comes to quotes it
>> brakes, since it doesn't retain the quotes. I.e., if I execute
>>   kd list -w "foo bar"
>> I want it to be translated into a call of kd-list like this:
>>   kd-list -w "foo bar"
>> but with the approach from above I get
>>   kd-list -w foo bar
>> and kd-list sees only "foo" as a -w's parameter, not "foo bar".
>>
>> How can I retain the quotes?
>
> Use double quotes around variable expansions.

Sorry, I don't get what you mean.
From: Dominic Fandrey on
On 24/02/2010 15:50, Tobias Nissen wrote:
> Janis wrote:
>> On 24 Feb., 15:30, Tobias Nissen <t...(a)movb.de> wrote:
>>> How can I retain the quotes?
>>
>> Use double quotes around variable expansions.
>
> Sorry, I don't get what you mean.

command "$@"

or

command "$2" "$3"

Simply always use double quotes, there is no information loss within
quotes when variables are expanded. I think even binary data survives
this treatment.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Teemu Likonen on
* 2010-02-24 15:30 (+0100), Tobias Nissen wrote:

> command="$1"
> shift
> args="$@"
> ...
> kd-$command $args
>
> Now this works for simple arguments. But when it comes to quotes it
> brakes, since it doesn't retain the quotes.

Do this:

command=$1
shift
[...]
kd-"$command" "$@"

"$@" will expand to

"parameter 1" "parameter 2" "parameter 3" [...]

Each one is quoted separately.