r/bash • • Dec 02 '14

Using #! in your scripts

http://shiroyasha.github.io/shebang.html
0 Upvotes

24 comments sorted by

4

u/thestoicattack Dec 02 '14

The shebang command must be the first line of the file and can contain any valid path for the interpreter, followed by the arguments that will be sent with the invocation.

Some shells won't interpret multiple arguments after the shebang, however.

4

u/geirha Dec 02 '14

Some shells won't interpret multiple arguments after the shebang, however.

It's the kernel that uses the shebang. To the shell, it is just a commented line since it starts with a #.

5

u/geirha Dec 02 '14

Don't put extensions on scripts. Especially not .sh when the script isn't even an sh script.

4

u/nath_schwarz Dec 02 '14

Don't put extensions on scripts.

Bullshit. If there is a suffix or not doesn't matter for the environment, it only matters for you if you need to find every *zsh or *bash-script on your system or in a specific folder.

If you're working with multiple shells you'll need that. If you only use one it doesn't matter.

What if you have bash and python scripts? If you want to search and list only your python-pieces you'd need to go into every file and grep the first line to see if it uses #!/usr/bin/env python or #!/usr/bin/env bash. So, instead of find -type f \*py you'll need to use grep (slooow) or silver searcher/ack (and even these will be slower since you need to access the file every time - not only the filename).

Especially not .sh when the script isn't even an sh script.

That, on the other hand, is correct.

4

u/cpbills Dec 02 '14
find <path_where_scripts_live> -type f -exec file {} \;

I have never had to find a script, simply because it was a python, bash, perl, or other script. The language doesn't matter. On the other hand, I do often look for bits of code, so I just grep for the pattern I'm looking for, in my directory of scripts.

1

u/[deleted] Dec 02 '14

On a side note: Check out the file command on Linux. You don't have to do all the madness grepping for shebang lines.

1

u/sbicknel 1,$s/n\?vim\?/ed/g Dec 08 '14

Exactly.

for f in *; do
    if ! [[ $(file -b --mime-type "$f") =~ perl ]]; then
	    continue
    fi
    ...
done

1

u/sbicknel 1,$s/n\?vim\?/ed/g Dec 08 '14

The argument against using .sh or .pl or .py, etc. is that if you have scripts that are used by other programs and you later decide to rewrite them in a different language, like changing from sh to perl, you must either change all references to the script you named with .sh to .pl, or you must leave it alone. Either way, you have a problem. If you make the change, you risk missing one or two references in out-of-the-way places. And if you don't make the change to its file name, your file's name becomes a big fat lie. But if you leave off the suffix altogether, you avoid both problems and your other scripts will never know the difference.

1

u/nath_schwarz Dec 08 '14

1

u/sbicknel 1,$s/n\?vim\?/ed/g Dec 08 '14

How does using symlinks change a file's searchability or help identify what a file is (that's what the file command is all about, by the way)? What is your reasoning for using symlinks when a script is system-dependent, and what do you mean by system-dependent?

Even if you use symlinks to the original script, if you change that script's implementation language and have it named with a suffix indicating that language, renaming it will break all of the symlinks, and not renaming it will mislead users about its content. Symlinks do not solve these issues.

1

u/nath_schwarz Dec 08 '14

How does using symlinks change a file's searchability or help identify what a file is (that's what the file command is all about, by the way)?

I.e. we have the script do_serious_stuff.py, which will be referenced by many other programs - this will be symlinked to do_serious_stuff. This way, we can add a new version do_serious_stuff.0.1.py (or even a version in another language do_serious_stuff.0.1.pl) without a problem and then force-symlink do_serious_stuff to the new file. Easy. And also - thats how its done with libraries and thats how its done in production systems and thats how its done with webservers and ....

What is your reasoning for using symlinks when a script is system-dependent, and what do you mean by system-dependent?

System-dependent is a script of which your particular system is dependent.

Even if you use symlinks to the original script, if you change that script's implementation language and have it named with a suffix indicating that language, renaming it will break all of the symlinks, and not renaming it will mislead users about its content. Symlinks do not solve these issues.

Wrong. As I said in the example above. Thats why we have symlinking in the first place. We have symlinking because the BSD guys were fed up with exchanging the real files all the time, so instead they created symlinks that can point to the correct version of the file.

2

u/Sigg3net Dec 02 '14

Don't put extensions on scripts.

Why?

7

u/geirha Dec 02 '14

Don't put extensions on scripts.

Why?

Same reason you don't put extensions on other commands. On linux systems, the command to list files is usually an ELF binary; but it's still named ls, not ls.elf, because the user running doesn't care or doesn't need to know what language was used to implement the command.

And what if you wrote a command, using sh, and named it foo.sh, but later realized it should rather be written in perl, then you have a problem. Do you keep the name as is, having now a perl script with a misleading .sh extension? or do you rename it to foo.pl and break everything that were relying on foo.sh?

See also http://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful

2

u/Sigg3net Dec 02 '14

Thanks!

I did actually remove .sh from my first public script but added it on later ones to avoid confusion. I'm interested in adhering to good coding practices/standards so I'll read the link you provided.

2

u/dhibbit Dec 02 '14

I keep a list of links without extensions that point to the files (which have them).

Whenever I create a new script or rename an old one, I simply run a script which updates the list of links.

Yes, it's another step, but it means I can type (and execute) either "script" or "script.sh" and all other scripts call the extension-less script name so that I can change from script.pl to script.sh to script.py without any difficulties.

1

u/cjwelborn Dec 20 '14

I use extensions on the actual scripts because I have a wide variety of languages that I use (bash, python, js, etc.). Most are in their own directory under ~/scripts/[insert language]/, but some are not (they are older than my "filing system" itself). Anything that I use a lot has a symlink in my ~/bin folder without the extension. So if I change languages for the command (which I have done before) I only need to change what the symlink points to. I also have one that will check all of my symlinks to make sure they are not dead links (they never are).

0

u/nath_schwarz Dec 02 '14

And what if you wrote a command, using sh, and named it foo.sh, but later realized it should rather be written in perl, then you have a problem. Do you keep the name as is, having now a perl script with a misleading .sh extension? or do you rename it to foo.pl and break everything that were relying on foo.sh?

That is a lack of planning and not a reason for bad practice.

7

u/thestoicattack Dec 02 '14

I don't think that every decision to rewrite a script can be called "lack of planning."

0

u/nath_schwarz Dec 02 '14

If you just started and then got to the point that it should be written in another language (e.g. because it is simpler or provides packages that do something for you) it doesn't matter, since the script shouldn't be used already (since it is not finished).
If you need to add something new much later, that needs a rewrite of the script in another language then it is a lack of planning that you can't easily change the calls to the script.

2

u/treerex Dec 02 '14

If you need to add something new much later, that needs a rewrite of the script in another language then it is a lack of planning that you can't easily change the calls to the script.

That may be the case in whatever utopian world you live in, but in the real world needs change over time and a script may evolve in ways that could never have been foreseen and that may require reimplementation in a suitable language.

1

u/nath_schwarz Dec 02 '14

And thats why its a lack of planning when you can't easily change the callings of said script.

Or - as said earlier, its a lack of planning because such a critical script isn't referenced with symlinks.

It happens, yes, no doubt - but that doesn't mean that its good practice.

3

u/ashep24 Dec 02 '14

Lack of planning can cause you to have to re-code a file in a different language. That doesn't mean allowing this flexability is bad practice. Even Google suggesting not using file extenstions for executable scripts.

4

u/nath_schwarz Dec 02 '14 edited Dec 02 '14

Googles reasoning is, that the system doesn't need the suffix - which is correct (as I said here). But imho the searchability and the information of what exactly the file is, is more important than not having to change the calls for this particular script.

If it is a particular system-dependent script being called by numerous programs you should consider using symlinks anyway.

Edit: Additionally, Google's shell scripting standard only allows bash-scripts, which makes the suffixes useless for google.

1

u/ashep24 Dec 02 '14

Good point, that makes sense for searchability. I think there's enough discussion here to help future readers make an informed decision for what will work best in their environment.