Shell – What’s the difference between sh script and sh < script

io-redirectionshell-script

What's the difference between

sh myscript

and

sh < myscript

I played around and they seem to have the same effect. Are they equivalent?

Best Answer

They are not. There are some things that will not work. The one thing that comes to mind is:

sh myscript -flag1 -value=3

is not possible with sh < myscript.

With sh < myscript, $0 is set to sh, rather than myscript, so

echo this script is $0

won't work

Also, if your script reads input, it will not work - you cannot

sh < myscript < myfiletoread