Solved 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
 
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).
Btw, can you tell what kind of script you are working on? From my experience, this is generally not a very good idea to cd in the script that implicit way - it may be very bug-prone. Now you can easily screw up you original script by making changes (or mistakes) in you sourced file. You also now should remember that sourcing that script affects your current directory and keep it in mind when you refer to files.

In most cases it's easier to use absolute paths - that would be much easier to manage. I tried both ways and I personally would stick with absolute ones. But again, each case is different, I don't know what your script is about.

In case you would go with sourcing the script and using functions, you may find handy to set she-bang for ./script.sh (slave in my example) to #!/usr/bin/false - in this case you will be able to source this script but will not be able to call it manually (since it is more likely not a standalone script, but rather a script with subroutines).
 
pkg install bash

Does it really act differently in bash as well?
This IS in bash.
Why do you think a shell behaves differently on FreeBSD compared to Linux?
Probably security? It seems like a proper security design to run scripts in a subshell.
Btw, can you tell what kind of script you are working on? From my experience, this is generally not a very good idea to cd in the script that implicit way
Importing a zfs pool...so I naturally want to cd into it right after I import it with this script.
I also suspect that OP talks about some bash-specific feature, not 'Linux-specific' one, since in most Linux distros shells/bash is the default shell, but in FreeBSD it is a sh(1).
So is this behavior specific to shells? (It's different behavior if I run it with /bin/sh?)
 
Back
Top