Skip to main content
improve *accuracy* of answer
Source Link
Seamus
  • 3.1k
  • 2
  • 19
  • 37

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

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:

< filename

This does exactly what you want and is easy to remember.

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

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, 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

missing apostrophe
Source Link
Jeff Schaller
  • 67.7k
  • 35
  • 118
  • 255

Even though everybody uses cat filename to print a files text to the standard output first purpose is concatenating. From catscat'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:

< filename

This does exactly what you want and is easy to remember.

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 wontwon't be printed
  printf "%s" "$(<$fname)"
  break
done

For further reading:
BashPitfalls - cat file | sed s/foo/bar/ > file
Bash Reference - Redirecting

Even though everybody uses cat filename to print a files text to the standard output first purpose is concatenating. From cats 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:

< filename

This does exactly what you want and is easy to remember.

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 wont be printed
  printf "%s" "$(<$fname)"
  break
done

For further reading:
BashPitfalls - cat file | sed s/foo/bar/ > file
Bash Reference - Redirecting

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:

< filename

This does exactly what you want and is easy to remember.

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

added 89 characters in body
Source Link
crunsher
  • 772
  • 5
  • 5

Even though everybody uses cat filename to print a files text to the standard output first purpose is concatenating. From cats 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:

< filename

This does exactly what you want and is easy to remember.

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 wont be printed
  printf "%s" "$(<$fname)"
  break
done

For further reading:
BashPitfalls - cat file | sed s/foo/bar/ > file
Bash Reference - Redirecting

Even though everybody uses cat filename to print a files text to the standard output first purpose is concatenating. From cats 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.

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 wont be printed
  printf "%s" "$(<$fname)"
  break
done

For further reading:
BashPitfalls - cat file | sed s/foo/bar/ > file
Bash Reference - Redirecting

Even though everybody uses cat filename to print a files text to the standard output first purpose is concatenating. From cats 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:

< filename

This does exactly what you want and is easy to remember.

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 wont be printed
  printf "%s" "$(<$fname)"
  break
done

For further reading:
BashPitfalls - cat file | sed s/foo/bar/ > file
Bash Reference - Redirecting

Source Link
crunsher
  • 772
  • 5
  • 5
Loading