Pala
Pala

Reputation: 2191

Shell script is printing parameeter without passing to it

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

Answers (3)

Faron
Faron

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

rk 1
rk 1

Reputation: 36

pass junk as parameter. so that the positional parameter will replace with it.

Upvotes: 1

John Kugelman
John Kugelman

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 in PATH need not be executable. When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the sourcepath option to the shopt builtin command is turned off, the PATH 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

Related Questions