You are not logged in.

#1 2012-11-18 23:15:41

Ypnose
Member
From: Jailed in the shell
Registered: 2011-04-21
Posts: 353
Website

.bashrc: cleaner & faster way to add functions

Hi archers,

My .bashrc file becomes filled of many useful functions. It's kinda messy and I don't think this is very clean. I wonder if I could add these functions to a separate file.
Actually I know how to do the trick but what about speed?
Does it take more time to load a bashrc with many functions or include a separate file in the bashrc?
For you, what is the best thing (cleaner & faster as the title says)?

Thank you.


Github -- My terminal font Envypn

Offline

#2 2012-11-18 23:30:20

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: .bashrc: cleaner & faster way to add functions

You could always split them out, source .bash_functions and do some benchmarking yourself...

My guess is that the difference will be in time intervals so small that only Spiderman will be able to notice.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2012-11-18 23:32:31

initbox
Member
Registered: 2008-09-27
Posts: 172

Re: .bashrc: cleaner & faster way to add functions

You might not always need all the functions you have in your .bashrc in every session, so it can be a waste of time to load them on every session needlessly.

I don't think it practically makes a difference whether you keep them external or include them in your .bashrc.

/usr/local/bin should be first in your path by default, put all your functions there as scripts. You don't need the extension if you have a shebang so you can keep the names clean.

Offline

#4 2012-11-18 23:46:29

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: .bashrc: cleaner & faster way to add functions

Or an option somehere in the middle so they can all be in one file but don't need to be source until used.

One script something like the following:

#!/bin/bash
first_cool_function() {
    # do stuff here
    # and here
}
second_cool_function() {
   # do other stuff
}
# more functions here

$(basename $0) $@

then make a link to it for each function name.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2012-11-19 00:51:19

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: .bashrc: cleaner & faster way to add functions

initbox wrote:

/usr/local/bin should be first in your path by default, put all your functions there as scripts.

I prefer $HOME/bin - this is user stuff after all, no need to stick it in a system dir.

Offline

#6 2012-11-20 22:44:52

Thaodan
Member
From: Dortmund, Nordrein-Westfalen
Registered: 2012-04-28
Posts: 448

Re: .bashrc: cleaner & faster way to add functions

tomk wrote:
initbox wrote:

/usr/local/bin should be first in your path by default, put all your functions there as scripts.

I prefer $HOME/bin - this is user stuff after all, no need to stick it in a system dir.

Yes but than ~/.local{bin,lib,share} woud be better.


Linux odin 3.13.1-pf #1 SMP PREEMPT Wed Mar 5 21:47:28 CET 2014 x86_64 GNU/Linux

Offline

#7 2012-11-20 22:57:05

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: .bashrc: cleaner & faster way to add functions

Ypnose wrote:

For you, what is the best thing (cleaner & faster as the title says)?

Yes, see my sig.

Last edited by graysky (2012-11-20 22:57:52)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#8 2012-11-21 11:14:17

Ypnose
Member
From: Jailed in the shell
Registered: 2011-04-21
Posts: 353
Website

Re: .bashrc: cleaner & faster way to add functions

@graysky: Actually, I saw your sig in one of your previous message and I visited your repo. That's why I created this subject.
I found interesting things. You seem to use two different shells, so adding aliases and funtions in separate files, is for me perfect.
It's not redundant. You dont need to add functions to both zshrc and bashrc.

I "benchmarked" my bashrc with "time" command. With the functions included in an other file, it doesn't really change.

@Thaodan: I have a $HOME/.bin folder added to $PATH for all my scripts. Using $HOME/.local{bin,lib,share} isn't very necessary. I don't use user specific libs since Arch provides me everything I need.

Last edited by Ypnose (2012-11-21 11:29:32)


Github -- My terminal font Envypn

Offline

Board footer

Powered by FluxBB