I was wondering whether I could make my own commands as in if I were to type in 'music' at the command line, my vlc should open up and start playing my playlist. The vlc thing is just an example but the question remains.

3 Answers
3

Next, type chmod +x music - this makes the file executable. You can now type ./music to run this script.

It's a good idea to type echo $PATH and copy the script into one of those directories. I would suggest creating $HOME/bin and adding that to your PATH via ~/.bash_profile or ~/.profile. This can be done by adding (to one of those files): export PATH="$HOME/bin:$PATH". You will then need to run source .bash_profile (assuming you put that in that file) to reload it.

Another alternative would be to add the command you want to run as a bash alias. You can do this by adding the following to a file named .bash_aliases in your home directory:

Any executable file in a directory in your path can be a "command". It can be a script as suggested in other answers, but it could also be a compiled program in any supported language you know.

Aliases will also work, but are limited because they are "one-liners" so that doing anything intricate with them quickly generates code that is very difficult to read. Also, since they execute in the same variable scope as the shell you type them into, they can easily have unintended side effects like leaving you in another directory or leaving variables defined that are only needed by the task the alias performs. (Of course, sometimes you might want to use an alias precisely because it does have desirable side effects.)

If you put your new commands in a directory in your path, they can be found by the shell and executed. The most common place to put them is in $HOME/bin. If this directory exists, it will often be automatically added to your path.

Two things to keep in mind when setting this up are:
1) It's usually best to use unique names for your commands that do not match any that are already defined on your system. There's a lot of debate on this, but the main point is that familiar commands should not have unexpected results if someone else is using your system and that getting used to a common command doing something special can get you in trouble when you work on another system where it isn't defined that way.

2) You can add $HOME/bin to the beginning or end of your path (or anywhere in between, but that's not usually done).

Adding it to the front of your path is the most convenient choice because your version of a command will always be the one that executes and it will be found faster. It is also a security vulnerability because anyone who can get write permission to your $HOME/bin (a lot easier than getting root access) can replace common commands with potentially malicious code very easily. Also, if you ever need to ask for help from someone who doesn't know your system, then they are likely to be tripped up by the special behavior of your custom commands unless you spend a lot of extra effort explaining everything on top of just asking your question.

Adding it to the end of your path statement is a little safer because any command with the same name as your command that's already in your path will be found first and run instead - and anything else that relies on that command working as expected will still work.

To avoid all of this (at the expense of a little extra typing), you can also put your command in a directory that's not in your path so it will almost never be run unintentionally because you'll have to type it's full path name to run it.

Like most things in Linux, there are many ways to create and use new commands and they each have their own advantages and limitations.