I have a simple Bash script:
#!/usr/bin/env bash
read X
echo "X=$X"
When I execute it with ./myscript.sh it works. But when I execute it with cat myscript.sh | bash it actually puts echo "X=$X" into $X.
So this script prints Hello World executed with cat myscript.sh | bash:
#!/usr/bin/env bash
read X
hello world
echo "$X"
- What's the benefit of executing a script with cat myscript.sh | bash? Why doesn't do it the same things as if I execute it with./myscript.sh?
- How can I avoid Bash to execute line by line but execute all lines after the STDIN reached the end?
 
     
    