Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

There are a few similar questions around that I've already seen, but I've come up with something different.

I'm reading this http://www.tldp.org/LDP/abs/html/invoking.html and I got confused when I read:

"Not recommended is using

sh scriptname

, since this effectively disables reading from stdin within the script."

There are two things here: first, I tried to execute a script like

sh myscript.sh < file.txt

and it reads from stdin fine.

The second thing is that I understand that adding shebang at the top of the script

#!/bin/bash

means exactly the same as

bash myscript.sh

, so I don't get what the author is pointing out there. I just got confused.

Anyon that understands what's the author's point?

Thanks so much in advance.

share|improve this question
1  
It's sh < scriptname that's the problem. –  Stéphane Chazelas Apr 23 at 10:43

2 Answers 2

You have a typo in your quote - It should start with "Not recommended is using sh <scriptname", which sends standard input to sh rather than to scriptname.

share|improve this answer
    
Oh I didn't see it! Thanks a lot! –  user3212942 Apr 24 at 13:49

The #!/bin/bash at the top of the script is useful only to execute it by typing directly it's name :

$ ./scriptname

That method also requires the file execution permission to be set. Indeed, when you execute a file, either it is written in assembly language, or the interpreter is specified in the file, following the convention #!:

#!/path/to_the/interpreter

Unless I am mistaken, text files that can't contain a such line (due to the grammar of the language they are written in.), can't be executed that way.
The only other way for such file, is to pass the filename as an argument to the interpreter. The main advantage of that second method is that it doesn't require the file to be executable for the user, but just readable.

For your first question, l0b0 is just right, you misread.

share|improve this answer
1  
One additional point on using the #! convention. You can add flags to the interpreter, e.g., #!/bin/bash -x or #!/bin/perl -w. This allows you to always have the flag set. –  Doug O'Neal Apr 23 at 19:34

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.