2

I'm fairly new to Linux, as many of you may have noticed. What I speak particularly is adding personal, user-generated functions and alias in my .bashrc file. I'm not exactly sure how bash works but according to many posts I "add it to the end of my .bashrc file." Which does not work when the command is executed through the Terminal.

1
  • Hi and welcome to the site! Your last questions (about how/where to learn) are off topic here. We only deal with specific, concrete problems, and requests for learning materials are too broad (I have therefore deleted that part of your question). Your first question is fine but please edit and show us an example of a function you've added, explain what it's supposed to do and how it is failing.
    – terdon
    Jul 8, 2014 at 11:06

2 Answers 2

1

For login shells, .profile in your home directory will be executed. So if you have functions defined in .bashrc, make sure the file is included in .profile as below:

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

Also, you can issue the command below:

source ~/.bashrc

and then call your functions. This will make sure there is no problem in including any files.

0

Whenever you open a new terminal, all the commands in your .bashrc are carried out (the file is 'sourced'). If you add a new command to your .bashrc, you need to either open a new terminal or issue one of the following commands:

. ~/.bashrc
source ~/.bashrc

For example, you could put on a new line (each command has to be on a new line -- well, that's actually a simplification, but it's enough for an absolute beginner to be getting on with) at the end of your .bashrc something like:

alias hello='echo "Hello, $USER"'

...then, once you've either opened a new, fresh terminal window or used one of the source commands, you should be able to type hello and get a greeting back from your machine.

If you are doing all this and the functions/aliases don't work, then there's probably something wrong with the specific functions or aliases that you're using. If you suspect that's the case, feel free to ask a separate question.

As for tutorials, one I found very clear and useful when I was starting out with bash was linuxcommand.

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