1

I want a bash script, call it args, to execute a command that is the arguments to the script.
In particular, I would like this command (note multiple blanks):

$./args echo 'foobar    *0x0'

to execute this precise command:

echo 'foobar    *0x0'

I tried this in args:

#!/bin/bash
set -x
$*

but it doesn't work:

./args echo 'foobar    *0x0'
+ echo foobar '*0x0'
foobar *0x0

Witness the single space, as well as moved single quotes.

With $@, the result is exactly the same, so please don't close the question on the account of differences between $* and $@. Also, blanks are not my only problem, there is the *0x0.

Mark Galeck
  • 6,155
  • 1
  • 28
  • 55
  • 1
    BTW, using a `.sh` extension for a _bash_ script is less than ideal; it implies that someone can run `sh yourscript`, but doing so won't make bash extensions available. Better to just call it `args` with no extension; see also https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/ (a friend's personal blog, but linked for over a decade from the #bash IRC channel's [factoid on the topic](https://wooledge.org/~greybot/meta/.sh) and thus somewhat authoritative). – Charles Duffy Jan 17 '23 at 23:39
  • 1
    Of the linked duplicate questions -- one's answers tell you that `"$@"` **with the quotes** behaves differently than either `$*` or `$@` without the quotes. It is correct in this. The other is even more direct about `"$@"` **with the quotes** being the correct solution to this issue. I stand by the duplicates, or at least, will need further explanation if you want to argue that they don't address the question. – Charles Duffy Jan 17 '23 at 23:41
  • @CharlesDuffy I see, thank you, I saw the various posts about differences between `$*` and `$@` and well, I tried to replace `$*` `$@` (without reading precisely in those posts that also double quotes needed, because, why would I read spend time to read them... title is obvious to try), and did not work. So there you go. Actually, I like it that you closed it because... I don't have to answer a question - I am lazy and I answer only 1 question for each 1 asked :) – Mark Galeck Jan 17 '23 at 23:43
  • @CharlesDuffy never mind, closed question is different than deleted question, damn it, I have to go and answer one – Mark Galeck Jan 17 '23 at 23:49

1 Answers1

2
#!/bin/bash
"$@"

This expands to all of the command-line arguments with spacing and quoting intact. $*, by contrast, is subject to unwanted word splitting and globbing since it's not quoted.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578