0

I am hoping to add some basic global functions that I can use in any directory. Is this possible?

Let's say I just wanted to do a simple test command:

say_hello () {
  echo "Hello World!"
}

Is there a certain file I can define this in so that I can use it across my entire computer?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 1
    Is there /etc/profile existing in OS X? This file is a normal way of defining a custom function for all users when logged in. – FrontENG Aug 14 '18 at 01:32
  • 1
  • Can you expand your question explaining what you mean by an "entire computer"? Do you want the function you've defined to become "visible" in different terminal windows owned by the same user(you), or for all users across the system? Do you want the change to persist after a restart? The suitable answer depends on these unknown variables. – undercat Aug 14 '18 at 02:31
  • 1
    @GageHendyYaBoy : Have a look at the chapter INVOCATION in the bash man page. It shows which files are sourced when a bash starts running. – user1934428 Aug 14 '18 at 08:36
  • @undercat sorry I was a little vague, just wasn't completely sure what info I should have been providing. I managed to place it in my .oh-my-zsh script that is loaded by hyper when opened. I think this is similar to a .bashrc. But that gave me my desired outcome. I can execute functions I define in there at anytime, anywhere on my system (just under my user). – Gage Hendy Ya Boy Aug 14 '18 at 16:22
  • @user1934428 I will check it out, I want to start learning some more about bash in general and that sounds like a great place to start! – Gage Hendy Ya Boy Aug 14 '18 at 16:23

1 Answers1

0

It sounds like you may want to modify your $PATH environment variable. You can do this fairly easily, but you should read man bash before you follow anybody's advice. bash reads several configuration files when it starts, and reading the man page will inform you of the order these are read.

Before describing the change, know where the change should go (which of the several config files). I'd recommend you use this one:

~/.profile (where ~ is just shorthand for your home directory),

or you may consider putting the change in this file:

~/.bash_profile

You should decide which one to use after reading man bash

And if the file you choose is not in your home directory, you will create it by entering:

touch ~/.profile (for example)

Before changing your path, you should know what it is now. This will tell you what the current path is:

echo $PATH

You should probably copy and paste that path somewhere (as a comment line in ~/.profile would be a good choice IMHO). This will make it easier to restore if things go south.

To change your path, you should add to it instead of replacing it. The order of the path variables is important because that's the order the shell will search. You can add to either the front end or the back end of the path, but for your case, I would recommend adding to the back end. Let's change it temporarily to begin with:

export PATH=$PATH:~/your/path (where your/path is the location you wish to append to your path)

For example if you want to add a folder in your home directory ~/myscripts to your path, this would be the command:

export PATH=$PATH:~/myscripts

You can check to see if that had any effect with:

echo $PATH

And you should see the path you've just added at the end of the list of paths

And so if you're happy with that, open the file ~/.profile in your editor, and add the line you've just used at the end of the file:

export PATH=$PATH:~/myscripts

And that's it. the path that you've added will become part of your "environment" each time to start up the shell. If you want to change it again, just edit ~/.profile to reflect your tastes.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Seamus
  • 2,925