1

I am new to linux, and I am learning bash scripting at the moment. I was wondering how I would save my script as a command and then be able to call that command anywhere in the terminal. So i could save the command in a folder somewhere, but be able to use that command in a different folder without having to type out the path to said command.

Is there a way to do this? For example, I make a script that runs chmod +x 'file' and then takes an argument for the file to run the command on. How would i go about coding this and then saving it so i can call that script from anywhere.

Sorry if i didn't explain my question too well, and thank you for any help with my problem.

Ryan Hosford
  • 113
  • 2
  • 1
    On my machine I have bin folder in my home folder ( /home/username/bin ) and it's supposed to be part of $ PATH variable. If you do echo $PATH it will show up there. So if you place the scrip into a folder that is part of $ PATH you can use it anywhere. Can't explain much in detail since I am on phone right now but that's the basic idea – Sergiy Kolodyazhnyy Oct 10 '14 at 02:41

1 Answers1

1

Let's assume that you often place random scripts in ~/bin as one of the comments mentioned. Just open your ~/.bashrc (provided that BASH is your shell of choice) and as the last line add:

export PATH="${PATH}:~/bin"

and that's pretty much it. You can add any directory to your PATH environment variable this way and it will allow you to run any executable file from any location on this system. If you want to add more directories, just prepend each one with a colon, i.e.

export PATH="${PATH}:~/bin:otherdir:yetanother"

Just remember that this will only work once you open the terminal again or if you don't want to, just reload BASH configuration with:

source ~/.bashrc