How to execute a command that is the arguments to the script?

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

$./args.sh echo 'foobar    *0x0'

to execute this precise command:

echo 'foobar    *0x0'

I tried this in args.sh:

#!/bin/bash
set -x
$*

but it doesn’t work:

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

Witness the single space.

With $@, the result is exactly the same, so please don’t close the question on the account of differences between $* and $@

>Solution :

#!/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.

Leave a Reply