-   Linux - General (
-   -   Creating a Linux Terminal Command From Scratch (

realpockets 10-26-2010 03:37 PM

Creating a Linux Terminal Command From Scratch
Hi everyone,

I am new to the Linux OS and I am very intrigued in learning all aspects of it. Currently I am using the Fedora 13 distro.

I am looking into creating my own terminal command from scratch. I researched online for some information on this subject was only able to view stuff related to OS X. I understand that they are both Unix based, are they both similar in creating these commands?

Basically all I would like is for someone to point me towards the right direction to start or complete this task.

Thanks for your help and support,


sycamorex 10-26-2010 03:42 PM

Hi and welcome to LQ.

You should start your script with:


followed by other commands. You'll find the following tutorial very useful:

realpockets 10-26-2010 03:50 PM

Thanks for the quick reply *sycamorex.

Quick question, are linux commands such as 'mkdir', 'sed', 'rm', 'chown' ... normally first written as bash scripts and then added to the command directory so I can call them via terminal?

Ultimately I would like to create a command and not a script.


*** this is what i mean ***

sycamorex 10-26-2010 04:30 PM

No, 'mkdir', etc. are not scripts. They are binary files written in C so that's what you could
start off with.

John VV 10-26-2010 05:50 PM

you might want to do some reading ?
you sound just a bit confused about things
start with DangerMouses site - for fedora
the fedoraforum

The basics for ALL Linux OS's

cin_ 01-17-2011 08:47 PM

advent command
Create the script.
Save copies of your scripts to a directory of your choosing.
You can then add the directory of the scripts to your Path...

# PATH=$PATH:/dir/to/scripts
# export PATH

...then you simply call them using their filename.

Once you have a directories for your homegrown commands you can try this...

I wrote a command that takes the project I am working on and makes it a callable command.
I usually work in a different directory than the one I have appended to PATH so it has proved much easier to simply call this command instead of typing the entire path to my script directory. I like to keep the languages separate, and I bounce around a bit so I just added a second argument defining the type of script it is.


# ungaunga
bash: ungaunga: command not found
# advcomm ungaunga b
# ungaunga
mah monkeh#


case "$2" in
        "b") chmod +x $1 && cp $1 /dir/to/scripts/bash/;;
        "c") cp $1 /dir/to/scripts/c/;;
        "p") cp $1 /dir/to/scripts/perl/;;
        "py") chmod +x $1 && cp $1 /dir/to/scripts/python/;;
        "r") cp $1 /dir/to/scripts/ruby/;;
        "l") cp $1 /dir/to/scripts/lua/;;
        *) echo -e "\nusage: `basename $0` sets command as callable\n: b bash : c C : p perl : py python : r ruby : l lua : \n";;

jmc1987 01-17-2011 10:28 PM

Just a better understanding Mac OS derived some some Unix code but Linux did not derive from Unix. Linus Torvolds designed the kernel from scratch to resemble minux. So that makes it a unix clone.

But just another option. You can make alias's to linux commands. For example if you wanted

makedir to do mkdir you just add a alias in your bash configurations directories either in your home directory or your system wide directory.

lumak 01-17-2011 10:42 PM

Just read "advanced linux programming" it's offered as a free pdf file or you can buy the book.

The question you are asking is too complicated to be asking on a 'linux general' forum.

Once you have programmed a little and need help with very specific programming question, go to the programming forum.

However, chances are, all you need is to execute a few existing commands in sequence. All you need for this is a bash script.

BTW, programming takes a lot of knowledge and understanding. I would also recommend learning at least C++ as this is a standard and generic place to start.

MTK358 01-18-2011 09:47 AM


Originally Posted by realpockets (Post 4140108)
Quick question, are linux commands such as 'mkdir', 'sed', 'rm', 'chown' ... normally first written as bash scripts and then added to the command directory so I can call them via terminal?

Of course not! How can you write a shell script that removes a file if you don't have rm in the first place?


Originally Posted by realpockets (Post 4140108)
Ultimately I would like to create a command and not a script.

I wanted to clear up the confusion between the terms command, script, binary, and program.

A program is a set of instructions.

A script is a program written is a scripting language (a scripting language is where there's a separate program that interprets the script every time you use it, for example Bash or Python).

A binary is a program written in a compiled language (such as C or C++) that's then been compiled. The binary runs directly on your CPU with no intermediate program.

A command is a program that's in a directory mentioned in your PATH environment variable, so that it's executed when you just type its name in a terminal.

cin_ 01-18-2011 11:15 AM

for posterities sake
As MTK358 explained :
Commands are globally callable programs due to their location being in the directories of your PATH.
How do they get there?
GNU creates essential programs like bash and their coreutils package.
Then each distro chooses carefully the programs that they believe are useful and want in their distro.
In this way your access to programs begins with the distro developers decisions.
Next, since GUIs are considered a standard at this point, the Desktop Environment, or the Window Manager, when loaded will carry with it software it feels is important.
Next, when you want some functionality and you find someone who has a project that fulfills this functionality and you retrieve the program, and either by your package manager or your own compilation makes it a callable command by placing the right files in the right directories.
Finally, when you want some functionality and your research for a solution returns null, then you write your own program and add it to your PATH.

Binaries are files that require you to reverse compile or find the source of to be able to read and understand its functionality. I'd suggest looking into C.
You can get the source to some of the most powerful and ubiquitous programs here...

Scripts are programs written in scripting languages without compilation and so can be executed and have their source examined from the executable.
You make certain sacrifices with efficiency functionality and resource usage when working with scripting languages, but like lumak explained they are fine for executing simple command sequences, especially when those commands require modification often. I also like to prototype in a scripting language. I find it makes my C binaries come together more seamlessly.

This sounds vast and intimidating, but it really is simple.
My example of the simplicity of this process from before, shown in its entirety...


* echo "mah monkeh"  ###RUNS THE ECHO COREUTIL

# ls
# ungaunga
bash: ungaunga: command not found
# ungaunga
mah monkeh

Great resources...
GNU Manuals...
GNU Source Collections... NOTE: bash, coreutils

All times are GMT -5. The time now is 03:58 PM.