Traveling down the Unix $PATH

Unix Insider |  Operating Systems



Why is it that some commands can simply be executed, while others must be ./executed? In other words, why do some commands need a dot-slash in front of them to run? Rather than giving you a short answer, I am going to explore a couple of things and hope you find them enlightening.


If you create a new shell script, will you be able to run it with the first command below, or will you need to resort to the second?



$ newscript 
$ ./newscript 
$ 


Commands in Unix are either builtins or executables. Builtins are part of the shell you are currently running. Examples include echo, read, and export.


Any command that is not built in must be an executable. There are two types of executables: shell script languages, such as sh, ksh, csh, or perl, or compiled executables, such as a program written in C and compiled down to a binary.


Commands created by using an alias in ksh also break down into these two main categories, because the command is translated and then issued as either a builtin or an executable. The following examples create aliases for the builtin echo and the executable grep.


The shell can always locate builtins, because they are built in to the currently executing shell.


$ alias sayit='echo ' 
$ alias g='grep ' 


In each case, after alias substitution is completed, the command becomes a builtin or an executable.


You can use the type command to verify the nature of echo.


$ type echo 
echo is a shell builtin 
$ 


Now use the type command to check on grep.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question