Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading