Passing argument to a script while executing it within current shell

Hi Gurus,

I have written a script set_env.ksh to which I pass an argument and set the oracle login credentials based on the argument I pass.

The script has code as below.

ORACLE_SID=$1
DB_SCHEMA_LOGON=$DB_SCHEMA_USER/$DB_SCHEMA_PASSWORD@$ORACLE_SID; export DB_SCHEMA_LOGON;
echo $DB_SCHEMA_LOGON

It works well when I call it as below

$ set_env.ksh DEV

and also works when I call the script from within another script.

But when I run this script within the current shell as below,

. ./set_env.ksh DEV

it does not get the argument passed ($# returns 0).... Any idea why it behaves so ? How can I pass the arguments while executing the script from within current shell itself.

Any help appreciated.

Thanks,
Sabari Nath S

Which shell are you using?

As a workaround you could define and use a function for that:

set_env() {
  ORACLE_SID=$1
  DB_SCHEMA_LOGON=$DB_SCHEMA_USER/$DB_SCHEMA_PASSWORD@$ORACLE_SID
  export DB_SCHEMA_LOGON;
  echo "$DB_SCHEMA_LOGON"
  }
set_env DEV

My home shell is sh... But as radoulov told, I am using the setting inside a script (which has #!/usr/bin/ksh in the beginning) and executing the script in sh with DEV as the argument...

Yes, so you could do one of the following:

  1. Change your shell (ksh, bash or zsh should be fine).
  2. Use a function.
  3. Invoke the script like this (*):
set -- DEV
. ./set_env.ksh
  • The command will reset the positional parameters.