1

I want to write a script that generates commandline arguments for another program. Until know I have written a script that simply echo the arguments and use it in the following way:

external_program $(generate arguments)

This stops working as soon as the generated arguments require quotes. To be clear what I mean: The resulting function call should be equivalent to

external_program "First argument with spaces" "Second argument with spaces"

How can I write and call a script to achieve such behavior?

EDIT: The things that I cam change is the exact commandline to connect the output of generate with the input of external_program (which should be short because that is something that I will have to type in) and the generate script which can be as complex as required to do the job.

EDIT: The duplicates does not match my problem because I already know how to pass all input arguments to another script and I know how to print variables, but my question is how I can get the output of one program copied over to the input of another program, so that quotes string stay as one input argument of the second program. EDIT2: "input of another program" was bad phrasing. What I actually mean was commandline arguments of another program.

15
  • how does generate arguments work? what does it return? Commented May 15, 2024 at 18:01
  • I write the generate script, so it can work in any way that solves the problem. Commented May 15, 2024 at 18:02
  • I'd make generate print each argument in a different line, then you can pass that to xargs. Commented May 15, 2024 at 18:04
  • So you mean something like "generate | xargs external_program"? That is not the way I would like it to work, but I will test it, and accept it, if it works in my case. Commented May 15, 2024 at 18:08
  • 2
    FWIW, external_program $(generate arguments) is subject to word-splitting and glob-expansion, so it is a broken method that you should never use. Commented May 15, 2024 at 18:49

1 Answer 1

2

You can use xargs to achieve that.

Write generate so it outputs each argument delimited by a null character:

#!/bin/sh
# File: generate
printf 'argument1\0'
printf 'argument 2\0'
printf 'argument"3\0'

Then you can just do ./generate | xargs -0 external_program.

Example:

#!/bin/sh
# File: print_args
for arg in "$@"; do
        echo "Argument: '$arg'"
done
$ ./generate | xargs -0 ./print_args
Argument: 'argument1'
Argument: 'argument 2'
Argument: 'argument"3'

This will work regardless of the characters the arguments contain, whitespace or not (except for NUL, of course).

Sign up to request clarification or add additional context in comments.

4 Comments

What if $argument do contain an single quote? Under bash you could either use printf 'Argunent: %q\n' "$argument" or echo "Argument: ${arg@Q}"!
Someting like printf 'Argunent: %q\n' $(generate) (without xargs)
@F.Hauri-GiveUpGitHub What would be the problem if any of the variables contain a single quote? I do not know which variable to you mean but I do not see a problem with any.
@F.Hauri-GiveUpGitHub This code has no issues with single quotes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.