Command Line Arguments in Linux Shell Scripting

6 Responses

  1. nazir says:

    Thank you so much

  2. George says:

    Thank you Sir.

  3. Blaine says:

    You missed [email protected], which is what a competent script would use (specifically “[email protected]”) to iterate over the input params, since either “$*” or $* would mangle parameters with shell metacharacters (i.e. would not work any time that user needs to quote the input parameters).

    • Karthikeyan Subramanian says:

      can you show us with example please.

      • netvor says:

        If Command was called with eg. `Command “arg1” “arg two” “arg3″`, that’s three arguments.

        Saying `”$*”` in Bash means “a string with all the arguments joined by space. Saying `”[email protected]”`, means “an array with each argument”. (Kinda like spelling each argument but you don’t need to know how many there are.)

        Say you wanted to use first argument for some logic and pass rest of them to another command, let’s say it’s mysql. Now you could say, `foo=$1; shift` — that would “consume” the first argument (`arg1`) to `$foo`. Fine. Now your argument array only contains `arg two` and `arg3`.

        So now you want to pass these two arguments to, eg., “mysql”. But calling `mysql “$*”` just means calling mysql with *one* argument, that is, string `arg two arg3` which is something that basically never ever makes sense!.

        Calling `mysql “[email protected]”` is almost always what you want.

        Using non-quoted version of either syntax is even worse and pretty much useless: it just means that now the resulting value is subject to whole host of various bash expansions, word splitting (it’s `”argument” “two” “arg3”), etc. and may lead to unpredictable results based on eg. what is currently in the directory where the command is called.

Leave a Reply

Your email address will not be published. Required fields are marked *