Comments on: How to Pass Command Line Arguments to Bash Script https://www.linuxtechi.com/command-line-arguments-in-linux-shell-scripting/ Wed, 05 Oct 2022 14:15:59 +0000 hourly 1 By: netvor https://www.linuxtechi.com/command-line-arguments-in-linux-shell-scripting/#comment-14611 Thu, 12 Sep 2019 10:58:07 +0000 http://www.linuxtechi.com/?p=3133#comment-14611 In reply to Karthikeyan Subramanian.

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 `”$@”`, 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 “$@”` 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.

]]>
By: Karthikeyan Subramanian https://www.linuxtechi.com/command-line-arguments-in-linux-shell-scripting/#comment-6173 Sun, 14 Oct 2018 09:22:04 +0000 http://www.linuxtechi.com/?p=3133#comment-6173 In reply to Blaine.

can you show us with example please.

]]>
By: Blaine https://www.linuxtechi.com/command-line-arguments-in-linux-shell-scripting/#comment-5292 Sun, 22 Jul 2018 16:53:08 +0000 http://www.linuxtechi.com/?p=3133#comment-5292 You missed $@, which is what a competent script would use (specifically “$@”) 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).

]]>
By: George https://www.linuxtechi.com/command-line-arguments-in-linux-shell-scripting/#comment-2227 Mon, 20 Nov 2017 09:30:56 +0000 http://www.linuxtechi.com/?p=3133#comment-2227 Thank you Sir.

]]>
By: nazir https://www.linuxtechi.com/command-line-arguments-in-linux-shell-scripting/#comment-285 Thu, 20 Oct 2016 20:07:27 +0000 http://www.linuxtechi.com/?p=3133#comment-285 Thank you so much

]]>