Reputation: 2191
I am calling a script2.sh from script1.sh without passing parameter. But I am passing parameter to script1.sh. I am trying to print first parameter of script2.sh.
script1.sh
#! /bin/bash
PRM1=$1
source script2.sh
script2.sh
#! /bin/bash
PRM1=$1
echo "$PRM1"
output is
# source script1.sh hello
#hello
#
But Ideally
# source script1.sh hello
#
Can any one tell me why script2.sh is taking script1.sh parameter as it's parameter?
Upvotes: 1
Views: 73
Reputation: 1243
You are not giving your script1.sh any argument as it is expecting one. (A small note, I always close the white space in this line: #!/bin/bash
- not #! /bin/bash
). In your script1.sh, try this:
#!/bin/bash
PRM1=$1
./script2.sh $PRM1
Upvotes: 1
Reputation: 36
pass junk as parameter. so that the positional parameter will replace with it.
Upvotes: 1
Reputation: 361917
From the bash(1) man page:
SHELL BUILTIN COMMANDS
source filename [arguments]
Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. If filename does not contain a slash, file names in
PATH
are used to find the directory containing filename. The file searched for inPATH
need not be executable. Whenbash
is not in posix mode, the current directory is searched if no file is found inPATH
. If thesourcepath
option to theshopt
builtin command is turned off, thePATH
is not searched. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.
Upvotes: 3