I am a new Unix user and learning bash shell scripting. How do I display a text file stored in /tmp directory in a shell script?
There are many ways to display a text file in a shell script. You can simply use the cat command and display back output on screen. Another option is to read a text file line by line and display back the output. In some cases you may need to store output to a variable and later display back on screen.
Task: Display output using the cat command
Following sample script displays contents of a file called /tmp/input.txt:
#!/bin/bash FILE="/tmp/input.txt" echo "*** File - $FILE contents ***" cat $FILE |
Task: Store a file output in a variable
Following sample script displays nameserver IPs:
#!/bin/bash FILE="/etc/resolv.conf" OUT=$(awk '{ print $2 }' $FILE) echo "*** Nameserver IP ***" for ip in $OUT do echo $ip done |
Task: Read a text file line-by-line
See our previous tutorial for more information: