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'm still very new to Ubuntu. I'm using 12.04, and I'd like to know if there is a way to run this program/shell script without typing:

/opt/idea/bin/idea.sh

It's kinda tedious and I'm quite lazy. Any help would be appreciated. Thanks.

share|improve this question
    
You can always give the full path of the file. –  muru Sep 22 at 8:32
    
Thank you @muru, this I know, but it is a program I use often, so I would like to be able to run it with a simple command, not a long file path. –  Jonny Sep 22 at 8:33
    
nufailm.blogspot.com/2012/05/… this will help for idea :) –  dedunumax Oct 10 at 5:17

6 Answers 6

You can create a function in your ~/.bashrc:

some-name () {
    /path/to/your/file
    # or:
    #cd /path/to/your
    #./path
}

Or you can create an alias:

alias some-name='/path/to/your/file'
# or  
#alias some-name='cd /path/to/your/; ./file'

In both cases, you can run it by calling:

$ some-name

If the file doesn't depend on where it's running, consider adding it to your ~/bin:

mkdir -p ~/bin
cp /path/to/you/file ~/bin
# or mv /path/to/you/file ~/bin
# or ln -s /path/to/you/file ~/bin

~/bin, if it exists, gets added to your $PATH automatically. Then you directly call file:

$ file

(Bad choice of name though, consider calling it something less generic.)

share|improve this answer
    
Thank you @muru –  Jonny Sep 22 at 8:38
    
@Oli always give my the five minutes, I tend to add indents. :) It's just annoying as I need to trick the editor to use Ctrl-K for second-level indents. –  muru Sep 22 at 8:39
2  
+1 in particular for the solution of adding file to your ~/bin, which is what I usually do (for longer scripts). I'd have probably even put that one at the top :) Aliases are good for shortcuts to commands with certain parameters, e.g., alias l='ls -CF'. Though I have seen much more awesome uses of aliases. I don't tend to declare functions in ~/.bashrc (I prefer to have all my scripts organised in separate files in ~/bin). Though all this is a matter of taste, I guess :) –  Malte Skoruppa Sep 22 at 9:19
    
@MalteSkoruppa I just gave preference to the methods that most easily lend themselves to cd, because the script may depend on the working directory. :) Otherwise, yes, I tend to put files in ~/bin myself. –  muru Sep 22 at 9:23

You can add /opt/idea/bin to your PATH variable:

PATH=/opt/idea/bin:"$PATH"

After this you can run it with simply idea.sh.

You probably want to add this line in your ~/.bashrc file.

share|improve this answer
1  
also: if you make the script executable, the .sh extension can be left away (assuming you use the shebang at the top of the script). simply "idea" is enough then. –  Jacob Vlijm Mar 1 at 8:24
1  
@Jacob: you cannot leave off the extension, as that is part of the executable's file name (this isn't Windows, where magic like that causes other issues). In order to call the script with the name idea, you would have to create a link or alias with that name pointing at the idea.sh script. –  Jonathan Callen Mar 1 at 13:25
1  
@JonathanCallen Callen you certainly can! make the script executable, remove the .sh extension from both the filename and the command to run it, given the fact that you start the script with the shebang #!/bin/bash. –  Jacob Vlijm Mar 1 at 15:25
1  
@Jacob what I mean is without renaming the file, you cannot (and it appears this is part of a larger software package, so you shouldn't just rename files) –  Jonathan Callen Mar 1 at 21:29
    
@JonathanCallen Hi Jonathan, I cannot see the context of this script, but in general, if it is a "main" file, or a script to call another main application file (which it is I guess, otherwise there would be no reason to call it from the user interface) it is concidered bad practice to use the language extension. –  Jacob Vlijm Mar 1 at 21:39

You can create a launcher by using following command:

gnome-desktop-item-edit --create-new <path-where-to-save>. I t will open this window.

enter image description here

Name it whatever you like and in command box type following

sh -c '/opt/idea/bin/idea.sh' and save it.

Now you can run that file using newly created launcher

OR

You can create a .desktop file with following contents

[Desktop Entry]
Name=<whatever-you-want>
Exec=sh -c '/opt/idea/bin/idea.sh'     
Terminal=false
Type=Application
Icon='<path to an icon file if you want>'

Now save it with .desktop extension on any place.

Make it executable with this command chmod a+x <your-desktop-file>

Now double click to open it.

share|improve this answer

You can just create symlink. Create it in /usr/local/bin. All you need is to run command:

ln -s full/path/to/your/file /usr/local/bin/name_of_new_command

After that you should made you file executable:

chmod +x full/path/to/your/file

Now you should be able to run name_of_new_command at any time in you terminal to do ./file

share|improve this answer
    
Great thanks. Easiest method! –  Jonny Sep 22 at 10:22
4  
Note that you shouldn't place a symlink in /usr/local/bin that points to a script in a private home folder, such as /home/jack/myscript.sh, as only user jack would usually be able to execute it. The symlink will be visible for other users, but not the file it points to. On a single-user system this may not matter, but still, it is "good practice" to place scripts (or links to scripts) that all users can read in /usr/local/bin, and private, self-made scripts (or links to scripts) of a single user in their ~/bin folder. –  Malte Skoruppa Sep 22 at 11:27

We can define a function and a add hotkey by using bind command to call that. Open ~/.bashrc file and add these line to it:

# define function that opens your program in working directory
Openprog(){
    /your-Program/path/here
}

# bind hotkey to it (<F12>)
bind -x '"\e[24~":"Openprog"'

Now when you press F12, your program will launch.

Note: A quick way to determine the escape code:

Open your terminal and press Ctrl+V. Now press your favorite keyboard shortcut. The correct escape code should appear. Just make sure to replace ^[ with \e before adding the shortcut to, e.g. replace ^[[24~ with \e[24~.

share|improve this answer

We can also run /opt/idea/bin/idea.sh file directly using bash_aliases

Open ~/.bashrc file by running,

gedit ~/.bashrc

Remove the # before the lines and save it, so that the lines will look like,

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Now open ~/.bash_aliases file,

gedit ~/.bash_aliases

Add the below lines in that file and save it,

alias idea='cd /opt/idea/bin && sudo ./idea.sh'

Finally source the ~/.bashrc file,

source ~/.bashrc

Now you can run /opt/idea/bin/idea.sh file directly by,

idea
share|improve this answer

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.