bash - Behavior of sh script depending on how it is called -


a have simple sh script1 calls simpler script2. 4 different results number , value of arguments of script2 if run, in bash shell, ./script1, ./script1 1, . script1 or . script1 1.

could explain why, please?

the scripts are:

script1

#!/bin/sh sourcerc() {     echo "@script1: inside fuction"     . /home/user/test/script2 2 } echo "@script1: # of arguments = "$# sourcerc echo "@script1: outside fuction" . /home/user/test/script2 2 

script2

echo "@script2: # of arguments = "$# echo "@script2: argument = "$1 

their permissions

-rwxr-xr-x  1 user mygroup  209 may 14 15:32 script1 -rw-r--r--  1 user mygroup   38 may 14 15:29 script2 

and results are

user@machine:~/test$ ./script1 @script1: # of arguments = 0 @script1: inside fuction @script2: # of arguments = 0 @script2: argument =  @script1: outside fuction @script2: # of arguments = 0 @script2: argument =   user@machine:~/test$ ./script1 1 @script1: # of arguments = 1 @script1: inside fuction @script2: # of arguments = 0 @script2: argument =  @script1: outside fuction @script2: # of arguments = 1 @script2: argument = 1  user@machine:~/test$ . script1 @script1: # of arguments = 0 @script1: inside fuction @script2: # of arguments = 1 @script2: argument = 2 @script1: outside fuction @script2: # of arguments = 1 @script2: argument = 2  user@machine:~/test$ . script1 1 @script1: # of arguments = 1 @script1: inside fuction @script2: # of arguments = 1 @script2: argument = 2 @script1: outside fuction @script2: # of arguments = 1 @script2: argument = 2 

the posix standard . indeed not require accept arguments, instead using positional parameters of script/function calls it. explicit arguments allowed extension:

the kornshell version of dot takes optional arguments set positional parameters. valid extension allows dot script behave identically function.


Comments

Popular posts from this blog

javascript - three.js lot of meshes optimization -

smartface.io - Proper way to change color scheme for whole application -

Email notification in google apps script -