Reputation: 63
I want to read a string from file and print it as follow :
LINE=tr '\n' ' ' < $FILENAME
x1=$LINE
echo $LINE
but the command echo $LINE
display an empty line ?
Upvotes: 0
Views: 90
Reputation: 11018
As already pointed out by the other posters, to embed the output of the tr
command into another command (in this case LINE=...
), surround the tr
command by $(...)
. In bash this is known as command substitution.
LINE=$(tr '\n' ' ' < "$FILENAME")
In case you intend to use $LINE
as a sequence of parameters for subsequent commands (in this case echo
), then newlines are eventually replaced by space during word splitting. This would make tr
superfluous; you might as well do this:
LINE=$(cat "$FILENAME")
or even better:
LINE=$(< "$FILENAME")
Word splitting is not effective inside double quotes; so echo "$LINE"
would still require tr
to remove newlines.
Upvotes: 2
Reputation: 785126
You should be using command substitution like this:
LINE=$(tr '\n' ' ' < "$FILENAME")
But this will store tr's output into LINE
variable.
Upvotes: 2
Reputation: 29103
I think you need to put the call to the tr
command with backquotes:
LINE=`tr '\n' ' ' < $FILENAME`
Upvotes: 1