Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I created a .sh (or bash) file and I would like to make a command in GNOME-Terminal for the file. I know you run a .sh file by:

./file.sh

After you compile it, I also know all the command are stored in /bin/bash. But I can't seem to figure out how I can create a command that I can call when I am in any directory in the terminal, something like:

abc

would run abc.h, etc. Any suggestions?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Although Radu's answer is absolutely correct, I feel like it's a bit incomplete for the beginner that does not know the location of certain files, so here's a step-by-step on the second method (using ~/bin). If you would like to store the files somewhere else than ~/bin, follow step 1, otherwise, ignore it and keep in mind you'll be using ~/bin as your scripts folders.

  1. Open a terminal and run the following:

    nano .profile
    

    You should see, at the end of the file:

    # set PATH so it includes user's private bin if it exists
    if [ -d "$HOME/bin" ] ; then
        PATH="$HOME/bin:$PATH"
    fi
    

    If you don't see it, try using nano .bash_profile instead. Keep in mind you should be at ~/, that is, /home/YOUR_USERNAME

    This tells bash where user's scripts are. The default is /home/YOUR_USERNAME/bin which is "abbreviated" as $HOME/bin or ~/bin. You can then change it to any folder you'd like to store your scripts in. Mine is ~/.bin -- the same as the original, but hidden.

  2. If you already issued chmod +x script.sh just move your scripts to the folder in which you're going to store them - create it if it doesn't exist. Inside that folder, if you store your scripts as file.sh you'll have to run file.sh everytime, so store them as file -- no extensions.

share|improve this answer
    
I cant seem to move any file into the File System directory, any suggestions on how I can disable this setting? –  0101011 Apr 27 '13 at 0:16
    
I'm sorry, I don't understand your question. Can you rephrase it? –  Alex Apr 27 '13 at 0:37
1  
Yeah, I can't move the file to the /bin directory, because it blocks me off. BUT I figured it out sudo mv file /bin, thanks though –  0101011 Apr 27 '13 at 0:42
1  
@01100011_0101011_0101011 He was referring to /home/$USER/bin (~/bin), not /bin; you should put your user scripts in that folder in your home folder, not in /bin. –  user76204 Apr 28 '13 at 10:58
add comment

The shell maintains a list of directories where executable files (programs) are kept, and just searches the directories in that list. If it does not find the program after searching each directory in the list, it will issue the famous command not found error message. This list of directories is called your path. You can view the list of directories with the following command:

echo $PATH

You can add directories to your path with the following command, where *directory* is the name of the directory you want to add:

export PATH=$PATH:*directory*

A better way would be to edit your .bash_profile file to include the above command. That way, it would be done automatically every time you log in.

Most modern Linux distributions encourage a practice in which each user has a specific directory for the programs he personally uses. This directory is called bin and is a subdirectory of your home directory. If you do not already have one, create it with the following command:

mkdir bin

Move your script into your new bin directory and you're all set. Now you just have to type:

my_script

and your script will run.

share|improve this answer
    
Source: linuxcommand.org/lc3_wss0010.php –  duxk.gh May 17 '13 at 12:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.