Why exactly is bash <script different than bash script? When i try this using a script that contains:


In the first case, i get what i expected:


But in the second case, i need to type a ctrl-D and then i get:

script: line 2: hello: command not found
script: line 3: world: command not found

I assume this means in the second case bash does not read line 2 and 3 as input for cat but as instructions instead.

I thought bash arguments are handled simply but opening the corresponding file and using dup2() to set that script file to the file descriptor 0, so having a file as argument or as an input redirection shouldn't be too different.

Is bash instead reading the entire file and then executing it line by line?

1 Answer 1


In the first case, feeding the script to bash as stdin:

  • bash runs cat
  • cat reads the rest of the file from stdin and copies it to stdout

In the second case:

  • bash runs the cat command
  • cat reads from stdin (which isn't the file) until you press ctrl-d to close cat
  • bash reads and runs the next line...

Note that when you put a filename on the command line like this, typically the file is a bash script and considered a program itself.

An alternate way to do this would be to put this in your file:

cat <<EOF

In this case, stdin is redirected by the shell and cat is fed the lines up to the second EOF. This is sometimes called a HERE document. You can replace EOF (in both places) with whatever marker you want.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .