Best way to run script NOT in a subshell with params

In FreeBSD if you run ./script.sh it executes in a subshell. I want to change folder of the current shell at the end of the script, but this doesn't work unless you source the script. But if you source the script you can't pass parameters to it. So annoying! This is a headache for anyone who switched to FreeBSD from Linux.

What's the nice trick to pass params to a script and to run it in a current shell? There's gotta be some nifty FreeBSD workaround for this? (Not something clunky like setting environment variables, etc).
 
I want to change folder of the current shell at the end of the script, but this doesn't work unless you source the script. But if you source the script you can't pass parameters to it. So annoying! This is a headache for anyone who switched to FreeBSD from Linux.
Could you tell how this can be done easily on Linux and what exactly is different on FreeBSD?
 
In FreeBSD if you run ./script.sh it executes in a subshell. I want to change folder of the current shell at the end of the script, but this doesn't work unless you source the script. But if you source the script you can't pass parameters to it. So annoying! This is a headache for anyone who switched to FreeBSD from Linux.

What's the nice trick to pass params to a script and to run it in a current shell? There's gotta be some nifty FreeBSD workaround for this? (Not something clunky like setting environment variables, etc).
You can use functions in ./script.sh, then source it, and pass the parameters to function:

master
sh:
#!/bin/sh

echo "master dir at start: $(pwd)"
. ./slave; do_chdir /etc/rc.d
echo "master dir in the end: $(pwd)"
slave
sh:
#!/bin/sh

echo "slave script"

do_chdir()
{
    local dir="${1}"
    echo "slave: do_chdir(${1})"
    cd "${1}"
}

Code:
$ ./master
master dir at start: /tmp
slave script
slave: do_chdir(/etc/rc.d)
master dir in the end: /etc/rc.d
 
Back
Top