Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to learn bash. I am using ubuntu.

I want to add a function. I am not sure weather I need to create a.profile or .bash_profile file so I create both in the ~/ directory.

I add something like this in each one:

    echo "function ran"

I log out of my user and log back in, I run this in the command line:

me@host:~$ myfunc

which just returns:

myfunc: command not found

Not sure what I am doing wrong.

share|improve this question
are you on your own box, or a system admistrated box (i.e. at school, work?). Also, is the box vanilla or customized? may help in diagnosis – Roy Rico Jun 30 '10 at 18:04
Right after creating your .profile, it isn't implemented right away. Either start a new shell or type source ~/.profile – Jarvin Jun 30 '10 at 18:07
@Roy I am the administrator, I installed the OS and everything. But I am a total n00b in linux. – JD Isaacks Jun 30 '10 at 18:42
@Dan: He said he logged out and back in. – Dennis Williamson Jul 1 '10 at 1:14

Your ~/.profile or ~/.bash_profile are sourced by the login process. A shell that you run in a terminal window is run in a different process which is a descendant of the login process. Certain settings made by the login process are exported to child processes, but aliases and function definitions are not. Aliases and functions should be defined instead in your shell's rc file, ~/.bashrc in this case. That file is sourced by every interactive bash process.

As for whether you should put environment variable settings and other start-up code in ~/.profile or ~/.bash_profile, man bash says that login shells look for ~/.bash_profile, ~/.bash_login and ~/.profile, in that order, and read the first one found. So if you have both ~/.bash_profile and ~/.profile, only ~/.bash_profile will be read and ~/.profile will be ignored. Which you use is up to you, but if you plan to use shells other than bash, you may want to put your settings in ~/.profile and delete ~/.bash_profile.

share|improve this answer
A function can be exported (but, as you said, is not by default). – Dennis Williamson Jul 1 '10 at 1:16

On the command line just type:

function myfunc()




echo "function ran"




Call the function by typing the function like so:

$ myfunc

Remove the function by using:

unset -f myfunc

Check these out for more information:

share|improve this answer
This did work. I wonder why adding to the file does not. – JD Isaacks Jun 30 '10 at 18:31
@john put an echo defining john's functions now into your files to see when they are being defined. You probably want them in your ~/.bashrc – msw Jun 30 '10 at 18:49
@msw I am not sure what you mean. by echo defining john's functions now Maybe elaborate in your own answer? But I tried adding it to .bashrc and it didn't give me an error, but no output either. Just went to the next line as if I just hit enter – JD Isaacks Jun 30 '10 at 19:10
@John, please read up. Given your resume and accomplishments, these questions seem to be beneath you. You're clearly not stupid, and ignorance can be fixed. If you can grasp Illustrator, bash should be easy by comparison but still needs some up-front homework. peace out. – msw Jul 1 '10 at 3:31
function foo() is bad. – grawity Jul 1 '10 at 18:15

To see which files are executed in which order, you might want to put a singel echo or printf command in those files. Then you will see when they are executed.

# You could try this
echo "This will be printed"
printf "This will also be printed (%d)\n" 10

To see how echo and print works, you shall use the man(1) command and look into the man page for bash(1). The parenthesis after the commands tells you which section in the manual you find the info. 1 means an ordinary command. You should try these commands:

man man
man -k manual
man bash

In manual page for bash, search for section "SHELL BUILTIN COMMANDS" (try / and write what you search, or use space key). There is the information about the commands that are built into bash, like echo and printf.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .