Even though everybody uses cat filename
to print a files text to the standard output first purpose is concatenating.
From cat's man page:
cat - concatenate files and print on the standard output
Now cat is fine for printing files but there are alternatives:
echo "$(<filename)"
or
printf "%s" "$(<filename)"
The ( )
return the value of an expression, in this case the content of filename which then is expanded by $
for echo
or printf
.
Update (for zsh
):
< filename
This does exactly what you want and, is easy to remember, but doesn't work in bash
.
Here is an example that lets you select a file in a menu and then prints it.
#!/bin/bash
select fname in *;
do
# Don't forget the "" around the second part, else newlines won't be printed
printf "%s" "$(<$fname)"
break
done
For further reading:
BashPitfalls - cat file | sed s/foo/bar/ > file
Bash Reference - Redirecting