Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
Are you going to distribute them? –  muru Jul 26 at 22:52
    
No, it's for my own use. –  Patterson Jul 26 at 23:00
5  
Also if you want those scripts to be available to all users, /usr/local/bin may be a better choice. –  Salem Jul 26 at 23:02
5  
@Salem /usr/bin and /usr/local/bin should both be available to all users, but /usr/local/bin is better for executables that are not part of packages. –  gerrit Jul 27 at 1:13
1  
@rath I get syntax highlighting without the extension if I have the shebang set to #!/usr/bin/env bash or #!/bin/bash. –  Sparhawk Jul 27 at 5:54

3 Answers 3

up vote 39 down vote accepted

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.

share|improve this answer
1  
Thank you. Your answer was very enlightening! –  Patterson Jul 26 at 23:06
    
@Patterson You're welcome. –  Mikołaj Bartnicki Jul 26 at 23:08
2  
"Directory ~/bin is included in $PATH by default" - Since when? Anyway, ~/.local/bin is probably a better choice as it's a standard. –  nyuszika7h Jul 27 at 11:07
    
You mean "Libraries must have a .so extension", right? Not sh. –  Keith Wolters Jul 27 at 13:39
2  
@KeithWolters first, not me, but Google. Second, .sh, not .so, we are talking about shell scripts, not binaries. –  Mikołaj Bartnicki Jul 27 at 14:00

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.

share|improve this answer
1  
~/bin is added to the $PATH automatically if it exists, no need to add it manually. Just create the directory, log out and log back in. –  Sergey Jul 29 at 21:03

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?

share|improve this answer
1  
Or even just #!/bin/sh if you don't need the full bash shell. –  flickerfly Jul 29 at 21:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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