Should I save my scripts with the .sh extension?

I have some functional scripts and I want to copy to /usr/bin I want to use them as normal terminal commands. Is it a good practice to use them with the .sh extension or can I save them without extension?

Asked By: Patterson

||

No, it is not a good practice, you should keep your scripts without extension. Note, that scripts being part of packages doesn’t have a .sh extension, i.e. update-grub, not update-grub.sh. If you are still not convinced, then be advised, that Google Shell Style Guide says:

Executables should have no extension (strongly preferred) or a .sh extension. Libraries must have a .sh extension and should not be executable.

PS You don’t have to put your script into /bin. You can create directory ~/bin and put your script there. Directory ~/bin is included in $PATH by default, so scripts put there can be run as any other shell command.

Answered By: user280493

I second the recommendation to use ~/bin which gets automatically added to your $PATH,as Sergey said. Or /usr/local/bin, which may already be on the PATH.
However:

  • You are doing this for yourself. Use whatever you feel comfortable with. Indeed, I’d say keep the extension so that you’ll be reminded it’s your script you are running, since –
  • Extensions are uncommon in /usr/bin. In my system, I can find only two:

    $ dpkg -S `ls /usr/bin/*.sh`
    mtools: /usr/bin/amuFormat.sh
    gettext-base: /usr/bin/gettext.sh
    

    So if you are packaging, definitely leave out the extension.

Answered By: muru

Just put following line at top of file:

#!/bin/bash

So-that file will be automatically type : Shell Script without any extension!

Remember to give execution permission to file.

For putting script so-that can be run by direct command, visit: Where should I put my script so that I can run it by a direct command?

Answered By: Pandya

Your scripts and the commands that run them don’t need to have the same name

I agree with the opinions and advice in other answers here, in that typed commands shouldn’t have an extension (mycmd, not mycmd.sh), and that editors should be able to identify the file contents from the shebang line (e.g. #!/bin/bash).

However, if the scripts are part of a larger project, I find it useful to have file name extensions on them to remind myself whether the files in the repo are editable (e.g. scripts, source code), or compiled binary files.

To achieve both aims, you can symlink your scripts from the directory on your PATH. E.g. if ~/.local/bin is on your PATH:

cd ~/.local/bin
ln -s ../../path/to/code/project/src/mycmd.sh mycmd

# now mycmd is on the PATH, and calling mycmd runs the script mycmd.sh
cd somewhere/else
mycmd

This also allows you to do version control from ~/path/to/code/project/, without then having to copy the files into a directory on your PATH when you do a "release".

Answered By: SpinUp

putting .sh as extension won’t do any harm but its better to follow standards.

Answered By: Shezan
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.