$* What does $* mean? ...">

What "$ *" means at the end of a shell command means

In the sample shell script, the command was -

. <sourced_file.sh> $* 

What does $* mean? Thanks.

+4
source share
6 answers

$* expands to all arguments that were provided to the script in which it appears, or to the current shell function if it appears inside the function.

This is usually a misnomer because it breaks arguments containing spaces into multiple arguments. More correct is " $@ " , which retains the original arguments, even if they have spaces.

+10
source

$* - a variable containing all positional parameters starting from 1 (arguments of the current shell script)

man 1 bash :

The shell considers several parameters. These options can only reference; assignment to them is not permitted.

*

Expands to positional parameters, starting with one. When expansion occurs in double quotes, it is expanded to one word with the value of each parameter separated by the first character of the special IFS variable. That is, $ * is equivalent to $ 1c $ 2c ..., where c is the first character of the value of the IFS variable. If IFS is not specified, parameters are separated by spaces. If IFS is NULL, the parameters are concatenated without intermediate delimiters.

@

Expands to positional parameters, starting with one. When expansion occurs in double quotes, each parameter is expanded to a single word. That is, "$ @" is equivalent to "$ 1" "$ 2" ... If a double-quotation extension occurs inside a word, the extension of the first parameter is connected to the initial part of the original word, and the extension of the last parameter is connected to the last part of the original word. When there are no positional parameters, expand "$ @" and $@ to zero (ie they are deleted).

Usually you want to use " $@ " , though:

"$ *" is equivalent to "$ 1 $ 2 ...", while "$ @" is equivalent to "$ 1" "$ 2" ...

+1
source

$* is an alias for all arguments specified for the current script.

For example, if you run:

 ./test_script.sh arg1 arg2 arg3 

If you execute echo $* in test_script.sh, you will see: arg1 arg2 arg3

+1
source

Google bash $* gives you An extended bash script guide that gives you the answer. You should often choose " $@ "

+1
source

$ * - arguments

So, if you script "foo.sh"

 #!/bin/sh gethostip $* 

And you call foo.sh -d localhost

he will be the same as

 gethostip -d localhost 

Minbd, however, $ * will not play well with arguments that have spaces.

0
source

His "all arguments":

 $ cat >test<<EOF > echo \$* > EOF $ bash test foo bar foo bar 
0
source

Source: https://habr.com/ru/post/1390402/


All Articles