Scripts in ~/bin vs. functions in ~/.bashrc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripts in ~/bin vs. functions in ~/.bashrc
# 1  
Old 02-28-2009
Scripts in ~/bin vs. functions in ~/.bashrc

Hi there,


Anyone knows what would be the cons and pros of adding a script in ~/bin vs. a function in ~/.bashrc?

I'm not sure how the system keeps tracks of some of the settings loaded in ~/.bashrc (like functions and aliases). Would I be right in thinking that this would all be loaded into memory and might slow down the system if I go "function" crazy? Should I add only small functions in ~/.bashrc and keep bigger scripts in ~/bin?

Just looking for some opinions, guidelines and best practices.

Thanks,

Vic.
# 2  
Old 02-28-2009
Quote:
Originally Posted by victorbrca
Anyone knows what would be the cons and pros of adding a script in ~/bin vs. a function in ~/.bashrc?

Scripts take longer to execute (it requires forking and execing a new process), and they cannot change anything in your current environment unless you source them. Functions are run in the current shell.
Quote:
I'm not sure how the system keeps tracks of some of the settings loaded in ~/.bashrc (like functions and aliases).

Variables are local to the current shell unless they are exported.

In bash, functions may be exported so they are available to any commands you call.

Aliases are local to the current shell. I don't use them at all. As it says in the nash man page, "For almost every purpose, aliases are superseded by shell functions."
Quote:
Would I be right in thinking that this would all be loaded into memory and might slow down the system if I go "function" crazy?

I have more than 100 functions loaded; I don't notice any slowdown.
Quote:
Should I add only small functions in ~/.bashrc and keep bigger scripts in ~/bin?

Not necessarily.
# 3  
Old 02-28-2009
Thank you very much for the detailed information Chris.

May I add that I was at last year's (2008) Ontario Linux fest and watched your presentation. It was very interesting!! Smilie


Vic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call functions from other scripts

i have a file that contains functions and i want the functions to be available in another script. of course, the ideal situation here would be to put the functions in the same script, but i dont own these scripts. so I have to call the functions file from a different script. how do i... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Usage of #!/bin/sh vs #!/bin/bash shell scripts?

Some question about the usage of shell scripts: 1.) Are the commands of the base shell scripts a subset of bash commands? 2.) Assume I got a long, long script WITHOUT the first line. How can I find out if the script was originally designed für "sh" or "bash"? 3.) How can I check a given... (3 Replies)
Discussion started by: pstein
3 Replies

3. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

4. OS X (Apple)

When to use /Users/m/bin instead of /usr/local/bin (& whats the diff?)?

Q1. I understand that /usr/local/bin means I can install/uninstall stuff in here and have any chance of messing up my original system files or effecting any other users. I created this directory myself. But what about the directory I didn't create, namely /Users/m/bin? How is that directory... (1 Reply)
Discussion started by: michellepace
1 Replies

5. Solaris

Questions about /usr/local/bin & scripts.

Hi gentlemen. For what intended is the directory /usr/local/bin? In this directory are some script. I don't understand how these scripts being in this directory are started. Each time after registration of the user occurs start of these scripts. These scripts start applications. (7 Replies)
Discussion started by: wolfgang
7 Replies

6. Shell Programming and Scripting

Bash: Nested functions and including other scripts

Hello. I have the following problem with bash code: function fl1_load_modules_and_get_list() ........... for module in $FL_MODULES_TO_PROCESS do source "${FL_MODULE_DIR}/${module}/module.sh" done ........... } function fl1_handle_install { local... (12 Replies)
Discussion started by: FractalizeR
12 Replies

7. Shell Programming and Scripting

[/bin/sh] passing parameters with quotes between 2 scripts

Hi, I have a first shell script (/bin/sh) that receives some paremeters. This is only an example (there are more parameters in fact and this one is among them): -header "This is a test" This script calls a secund shell script (/bin/sh) with the same parameters. But, quotes disappear as I would... (0 Replies)
Discussion started by: velo_love
0 Replies

8. Shell Programming and Scripting

How to call C functions in shell scripts?

How can i call dynamic C functions in shell scripts? (5 Replies)
Discussion started by: agarwal
5 Replies

9. Shell Programming and Scripting

Calling functions in scripts directly

Hi, I have a menu driven script that does various tasks, I want to be able to call functions directly from within other unix scripts from my menu script. Is this possible? (12 Replies)
Discussion started by: LiquidChild
12 Replies

10. Shell Programming and Scripting

Using #! /bin/sh in Shell scripts

Hi All, What does #! /bin/sh mean in a shell script? Is it mandatory to include in a shell script? I'm able to execute the shell script without it. Any help on this would be appreciated. (4 Replies)
Discussion started by: sumesh.abraham
4 Replies
Login or Register to Ask a Question