Passing arguments from a shell script to a command

For future reference: In a shell script, if I have to pass an unknown number of command line arguments to another command, and some of the arguments might contain spaces, here’s how to do it:

#!/bin/sh
othercmd --foo --bar "$@"

The magic “$@” invocation in double quotes does the trick. The more natural “$*” doesn’t work because it doesn’t keep arguments with spaces intact.

(Thanks go to Paolo who actually figured it out.)

This entry was posted in General. Bookmark the permalink.

One Response to Passing arguments from a shell script to a command

  1. great tip! says:

    Thank you for sharing, I had been searching for this!