1

I don't exactly know the difference between a script and a few pages mention that there is almost no difference between them but I do wonder why to execute a command all we need to do is type its name and press enter but to execute a script a .sh follwos the name of the file .(If a command is also an executable file why isn't there any need for extensions in case of a command ? ) I think it is possible to create your own commands in shell as I work on a system and the names of few commands seem customized(they match the names of some people). If it is possible to create your own command then how can I view the code for a user made command ? And how can I make my own command ?

0

You have to make a difference between programs that are compiled to machine code from a compiled language like C and interpreted language like bash. A compiled program consists of machine code that the computer can run with no further help.

With a script, you need to tell the computer, which interpreter it should use. For example, you can run a bash script by putting the line #!/bin/bash first in the file. It tells the computer, that it should use the bash-shell as the interpreter. If you turn on the executable bit in the file permissions, you can run it from the command line like any other program. You can leave the .sh extension away.

  • So by putting #/bin/bash in the first line of a script like "abc.sh" will turn it into a command and just by writing abc and pressing enter we can get the required getting executed ? – private ryan Sep 5 '16 at 16:27
  • If the #!/bin/bash shebang is the first line of the file; it has the appropriate executable bit set; and it is in your path, then you can execute that script just by typing its name. You may call it abc or abc.sh or whatever you like. The .sh suffix is a convention, not a requirement. – user4556274 Sep 5 '16 at 17:05
  • @user4556274 : Thank you . And so I can find the code for that command in my directory . – private ryan Sep 5 '16 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.