My problem is that I can't print to the screen when I write a bash script

. myBashScript.sh < text.txt

In myBashScript.sh I just print out

echo $2

But instead of printing "text.txt", it show a 'newLine'. As I have been know that '<' is a new line.

Why is $2 not text.txt?

  • Was it ./myBashScript.sh, is that a typo ?
    – daisy
    Sep 14, 2012 at 13:29
  • no it's not a typo. Because a shell script run on subprocess so the main process is waiting for it until it done. However, subprocess run it but the results will not update. So I have been used an alias
    – cat916
    Sep 14, 2012 at 13:31

1 Answer 1


I assume you're actually typing

./myBashScript.sh < text.txt

with a forward slash.

When you run ./myBashScript.sh < text.txt, your interactive shell actually captures the < text.txt and interprets it as a special instruction – in particular, it interprets your command line to mean that it should run myBashScript.sh with standard input connected to text.txt. Your shell then removes < text.txt from the command line before passing off control to myBashScript.sh. So as far as your shell script is concerned, it receives zero arguments, and $2 is empty. Your script translates simply to


which prints a newline.

If you want to actually print the name of the file, you should consider

echo $1

which you can then run:

$ ./myShellScript text.txt

If, on the other hand, you want to print the contents of the file, you should use cat(1); your shell script should be

cat $1

which you can then run:

$ ./myShellScript text.txt
Hello from text.txt, a file containing a bunch of test strings.
  • thank you very much. Now i get better to understand shell script behind the scence
    – cat916
    Sep 14, 2012 at 13:43
  • Just following up the question, how can I read the content of file because cat display the content of this ?
    – cat916
    Sep 15, 2012 at 3:14
  • @minhcat_vo: Traditionally, Unix shell scripts don't read from or write to files; instead, they read from standard input and write to standard output, and the user relies on I/O redirection (< and >) to specify files to read and write. However, you can pretty easily read a file using command substitution: file_contents = "`cat /some/file`" will read the contents of /some/file into file_contents. Sep 15, 2012 at 13:28

You must log in to answer this question.

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