Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
Was it ./myBashScript.sh, is that a typo ? –  warl0ck Sep 14 '12 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 –  minhcat_vo Sep 14 '12 at 13:31
add comment

1 Answer

up vote 3 down vote accepted

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.
share|improve this answer
thank you very much. Now i get better to understand shell script behind the scence –  minhcat_vo Sep 14 '12 at 13:43
Just following up the question, how can I read the content of file because cat display the content of this ? –  minhcat_vo Sep 15 '12 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. –  Benjamin Barenblat Sep 15 '12 at 13:28
thanks @Benjamin Barenblat –  minhcat_vo Sep 15 '12 at 22:52
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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