0

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.

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.

1

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.

  • 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. – doneal24 Apr 23 '14 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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