7

I know how to create my custom terminal commands and that would be really useful for me in compiling all my java classes in a folder. I also see that for doing this people usually creates a folder ~/bin and put files .sh with the lines they wish they run, but I could not understand it well, I create a compile.sh with some lines for example

-gcc file -o file
-gcc file1 -o file1

but as I was reading I also need to add a line to a file called .bashrc that is hidden in my home folder and I don't know how to do that, is there any way easier of editing this file? After I do it, my custom command will be the name of the .sh file (compile)?

Braiam
  • 66,947
  • 30
  • 177
  • 264
Vitor Falcão
  • 221
  • 2
  • 3
  • 10
  • 1
    I think you're interested in basic shell scripting; there are plenty of tutorials and resources online if you search for "linux basic shell scripting" (or similar). The existing resources can likely explain it more clearly and thoroughly than we can here – Nick Weinberg Jun 20 '16 at 21:57
  • 1
    Once you know how to create your own scripts, stop and find a simple tutorial for `make`. It's the right tool for controlling compilation of your projects. – alexis Jun 21 '16 at 00:59
  • Urgh, I hate the dupe hammer sometimes. The dupe is for this part: "[I] need to add a line to a file called .bashrc that is hidden in my home folder and I don't know how to do that, is there any way easier of editing this file" - what you mean to do is add your command to the `PATH`. – muru Jun 21 '16 at 05:32

3 Answers3

10

The easiest way to make Bash scripts that are available to all users of your computer is to place them in /usr/local/bin. This requires you to have admin privileges though.

  1. Create you script:

    Open your favourite text editor and write all the commands you wish the script to run, one command per line.

    You can add comments to Bash scripts by beginning a line with #.

    When you're done, add this exact line below as first line at the top of your script. It's called a "shebang" and tells the shell with which interpreter to execute your script.

    #!/bin/bash
    

    Here's a simple example of a full script, running the two commands you mentioned in your question:

    #!/bin/bash
    
    # Compile 'file':
    gcc file -o file
    # Compile 'file1':
    gcc file1 -o file1
    
  2. Move your script to the correct location:

    The location where you should place own scripts for all users is /usr/local/bin.

    As this directory is owned by user root, you must be an admin and use sudo to move files there.

    I assume you created your script in your home directory, ~/my_script.sh. Simply open a terminal and type the command below, replacing SCRIPTNAME with the name you want to give it. This name will be the command you have to type to run it. The .sh file extension is not necessary.

    sudo mv ~/myscript.sh /usr/local/bin/SCRIPTNAME
    
  3. Set the correct owner and permissions:

    The script should be owned by and writable for root, but readable and executable for everyone. The two commands below ensure this:

    sudo chown root: /usr/local/bin/SCRIPTNAME
    sudo chmod 755 /usr/local/bin/SCRIPTNAME
    
  4. Run your script and enjoy:

    Now you can simply type SCRIPTNAME (or however you called it) to your terminal and the script will get executed.

Byte Commander
  • 105,631
  • 46
  • 284
  • 425
  • 1
    a lot of this fooling around can be avoided by putting things in `~/.local/bin` instead; Ubuntu (and maybe Debian?) automagically links dot-folders in the home directory to where they would be off of `/usr/`. Main advantage there is your home directory becomes portable between upgrades (especially if mounting the home directory separately) so you never lose the custom little commands. – Andrew Keech Jun 20 '16 at 23:11
  • 2
    @AndrewKeech I just tried that and it didn't work. A better place for user scripts is `~/bin`, because Ubuntu will automatically add it to `$PATH`. See [here](http://askubuntu.com/a/402410/301745). – wjandrea Jun 20 '16 at 23:43
  • I did everything in this answer and worked really well to me but Andrew's way also didn't work here @wjandrea – Vitor Falcão Jun 21 '16 at 01:08
  • oh quite right! looking at my own $PATH I must have added `/home//.local/bin` myself. Whatever the case, I feel it's best to put anything that you make yourself somewhere in `~/` to keep it during wipe-clean upgrades – Andrew Keech Jun 21 '16 at 01:29
  • 1
    @VitorCosta wjandrea's suggestion will work; I also recommend putting custom scripts in `~/bin` rather than `/usr/local/bin` for portability. – edwinksl Jun 21 '16 at 02:20
  • Oh man, this whole page and many other places fail to mention that you may need to LOG OUT and log back in in order to make your command to actually work, at least this was the case for my Xubuntu. I wrote FOOL-PROOF INSTRUCTIONS for people like me to get your command to work: https://askubuntu.com/a/1077496/258952 – Manu Järvinen Sep 22 '18 at 14:19
  • It works any as @AndrewKeech and @wjandrea both said. Ubuntu automatically adds `~/bin` and `~/.local/bin` to the path, but only if they exist; that's why you need to logout and log back in, as mentioned by @ManuJärvinen (if those directories don't exist at the time you login, then they are not automatically added to `$PATH`). – Simón Nov 12 '21 at 02:59
  • I also agree if there's no reason for all users to have access to those scripts, `~/bin` and `~/.local/bin` should be preferred over `/usr/local/bin`. The only difference, I think, between `~/bin` and `~/.local/bin` is probably whether one intends those scripts to be roaming around (if such feature is available) or to stay only in the local host. – Simón Nov 12 '21 at 03:06
4

While writing scripts is a very convenient way to execute many commands from one file , I would suggest using functions where it requires just one or two commands.

Take for example function definition bellow

compile()
{
  gcc "$1" -o "${1%%.*}" && printf "<<< Compiled successfully\n"
}

By placing it into your .bashrc file (and then running source ~/.bashrc or opening new terminal tab) , you can run this command from anywhere without having ~/bin added to your PATH variable, and provide a filename as command line argument like so

compile somecode.c

On a side note, you can edit that file just by calling gedit ~/.bashrc from command line

For those of you wondering what does "$1" and "${1%%.*}" , the "$1" refers to the first command line parameter ( in this case "somecode.c" ), as for "${1%%.*}" - that's parameter expansion , particularly the one that does suffix removal , and will throw away anything after the dot. In other words , that turns somecode.c into somecode . For more info, read bash manual page section about parameter expansion

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
3

Before getting into this, I wanted to clarify that you're not creating new commands, you're creating scripts that can be run as executables. But they can behave in the same way as a command because you can run them from anywhere.

If you don't have a folder in your home folder called "bin", create one.

Now open a terminal window and type nano ~/.bashrc.

This will open an editor. At the bottom of this file, add a new line that says PATH="$HOME/bin:$PATH".

Now save the file by pressing CTRL+O (the letter o, not zero), then press ENTER. Exit the editor with CTRL+X. Logout and back in for changes to take effect, or restart your computer if that doesn't work.

Now any scripts you create in your bin folder will be accessible anywhere in a terminal window.

You can create these scripts with either nano, or gedit (the Ubuntu graphical text editor) or any text editor you wish. Just remember to save your script in the ~/bin folder and make your file executable. There 2 ways to do this:

GUI : Right click the file and go to the permissions tab, and check the box "Allow executing as a program".

TERMINAL : chmod +x /location/of/your/file

Delorean
  • 10,783
  • 1
  • 20
  • 42
  • This is good advice, but executable scripts *are* new commands. – alexis Jun 21 '16 at 00:58
  • @alexis I disagree. A script is a script. A script runs commands. Bash has a set of commands that you can run using a programming language in the form of a script. – Delorean Jun 21 '16 at 03:23
  • Some of those commands (which are _not_ provided by bash, but by the Unix environment) are themselves shell scripts :-) Have you looked in `/usr/bin` recently? Call them whatever you want, but don't correct the OP for calling them commands. – alexis Jun 21 '16 at 08:52