2

I want to know whether I can use different extensions for linux executables instead of using ".sh" or ".o" extension.

3 Answers 3

4

Yes, you can use anything you like.

Extensions do not matter on *nix systems. For binary files (like a compiled C program), the program will be self-executing. For executable text tiles, what matters is the shebang line at the top:

#!/bin/bash

This tells the shell to interpret the file with Bash, for example.

Of course, extensions are still relevant for graphical sessions.

4
  • Sorry to duplicate your answer: yours came through just as I posted mine.
    – AFH
    Oct 21, 2014 at 10:58
  • It does not matter for compiled programs either. It simply is part of the filename. Example: <pre> toad:/home/hennes>which bash /usr/local/bin/bash toad:/home/hennes>cp which bash . toad:/home/hennes>ls -l ./bash -rwx------ 1 hennes users 745832 Oct 21 14:07 ./bash toad:/home/hennes>mv bash bash.some_extension toad:/home/hennes>./bash.some </pre> Happily starts the bash binary despite changing the name
    – Hennes
    Oct 21, 2014 at 12:09
  • @Hennes Right, that's why I said "Unless it's a binary file..." (insert: "in which case it's completely irrelevant") "what matters is the shebang line".
    – slhck
    Oct 21, 2014 at 13:12
  • I read that as "unless it is a binary file in which case extensions do matter"). Different interpretation of the same sentence, possibly caused by a non-native reader (aka me).
    – Hennes
    Oct 21, 2014 at 13:30
1

Unlike Windows, Linux (and other Unix-like OSes) uses Magic Numbers to differentiate between different file types and consequently doesn't care about the file extension.

The magic number (sometimes file signature) is a pattern that is compared to a database in order to work out the file type.

You can use the file command to determine the file type:

$ file /usr/bin/bash
/usr/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x2af417cdc1684a2ab609fcab847dfee8cc643713, stripped

or:

$ file /etc/init.d/network
/etc/init.d/network: Bourne-Again shell script, ASCII text executable

If you rename a file and change it's extension, it doesn't catch it out:

$ cp /etc/init.d/network ~/network.jpg
$ file ~/network.jpg
/home/gareth/network.jpg: Bourne-Again shell script, ASCII text executable
$ mv ~/network.jpg ~/network.exe
$ file ~/network.exe
/home/gareth/network.exe: Bourne-Again shell script, ASCII text executable
0

Yes, any extension or none will work, provided it is a text file and marked as executable.

If you use #! plus the required interpreter as the first line, this will override the default script interpreter.

2
  • It doesn't have to be a text file. It can also be binary. In that case it'll be self-executing of course.
    – slhck
    Oct 21, 2014 at 10:58
  • Quite right: I misread the question as talking about scripts, which have been preoccupying me recently. Binary files have their "magic number" to identify the file type. Extensions are mostly relevant to files shared with other operating systems, though some are generated by default (eg .o).
    – AFH
    Oct 21, 2014 at 11:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .