How to make a shell script global?

MacosShellUnix

Macos Problem Overview


I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).

For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.

Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks

Macos Solutions


Solution 1 - Macos

/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default

Solution 2 - Macos

Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.

If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.

Solution 3 - Macos

There are two ways to do it -

  1. Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
  2. Create a folder in your home directory called bin. (For your personal scripts)
    • cd ~ (Takes you to your home directory)
    • mkdir bin (create a bin folder)
  • vim .bash_profile (to set path environment variable)
  • export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
  • Now you can just type the name of your script and run it from anywhere you want.

** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)

Solution 4 - Macos

In mac operating system

  • Open bash ~/.bashrc file.
  • add path of your script in your bashrc file , using export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
  • Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
  • open new terminal window Now whenever you will open your terminal your script will be loaded

Solution 5 - Macos

This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.

(I use Arch Linux Manjaro so I use .bashrc located in my home directory)

The code to be placed in your .bashrc file:

# Simple bashrc method to launch anything in terminal from any directory

YOURCOMMAND () {
  cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}

As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)

Hope I've helped someone!

Sincerely,

AnonymousX

Solution 6 - Macos

On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"

Now execute the script by function call in the shell.

Solution 7 - Macos

Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.

Solution 8 - Macos

from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)

Solution 9 - Macos

You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).

You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.

You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzebramanView Question on Stackoverflow
Solution 1 - MacosunbeliView Answer on Stackoverflow
Solution 2 - MacosBryan OakleyView Answer on Stackoverflow
Solution 3 - MacosPiquéView Answer on Stackoverflow
Solution 4 - MacosSher MohammadView Answer on Stackoverflow
Solution 5 - MacosAnonymousXView Answer on Stackoverflow
Solution 6 - Macospurushothaman poovaiView Answer on Stackoverflow
Solution 7 - MacosYouView Answer on Stackoverflow
Solution 8 - Macosd1gl3rView Answer on Stackoverflow
Solution 9 - MacosFrxstremView Answer on Stackoverflow