0

A typical bash setup, sourcees additional dotfiles on startup which obviously increases the startup time.

Is there an be any benefit of moving the contents of a function to its own executable file?

Current setup

~/.bashrc

#!/bin/sh
if [ -f ~/.bash_functions ]; then . ~/.bash_functions; fi 

~/.bash_functions

#!/bin/sh
function_one(){}
function_two(){}
....

Suggested Setup

~/.bashrc

#!/bin/sh
export PATH="$PATH:$HOME/.bash_functions/bin"
  • ~/
    • .bash_functions/
      • bin/
        • function_one
        • function_two
2

Actually, executing a script file will spawn a new shell process while sourcing a script doesn't, so your premise is wrong. Even if it wasn't:

1) The suggested setup won't work because the functions won't be visible outside the newly spawned shell.

2) The difference between execution time of a sourced script and an executable script is negligible.

3) The suggested setup will end up in having more executed files.

Shell scripts (which are interpreted) are anyway made for clarity and practicality and not for high performances such as compiled C programs. What basically you're trying to do here (in the wrong way!) is to tune a donkey cart.

  • What newly spawned shell are you talking about? – Barmar Jan 13 '17 at 16:29
  • @Barmar The scripts that would be in the proposed ~/.bash_functions/bin. – Gilles Jan 14 '17 at 15:24
  • I didn't think there would be functions inside the scripts, the scripts would replace the functions. I.e. the contents of each script would be the body of the corresponding function. The only problem would be if a function wants to change the shell environment. – Barmar Jan 15 '17 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.