0

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 ?

1
  • Now saying that would give error, won't it? Use command substitution. Commented Jan 15, 2014 at 18:05

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

$(...) does NOT replace newlines with spaces - the only thing it does is trim trailing newlines (however many). Your alternate solutions therefore do not work as intended. Also, please double-quote the $FILENAME references.
@mklement0: You are right; the truth about disappearing newlines is a bit more complicated. I made an edit to rephrase that.
Thanks for the edit, but it is important to note that word splitting is NOT involved during variable assignment - if the contents of $FILENAME has interior newlines, they will end up in the value of $LINE. It is only when you later reference $LINE without quoting that word splitting occurs - along with other shell expansions such as globbing, which may or may not be desired.
Thanks, I hope my edits sufficiently reflect your comments. Whether or not word splitting is applicable, depends entirely on the context (how will $LINE be used). OP was not explicit about that, so everybody will have to make up his own mind there.
Thanks - between your edits and this comment thread I think we've got it covered.
2

You should be using command substitution like this:

LINE=$(tr '\n' ' ' < "$FILENAME")

But this will store tr's output into LINE variable.

4 Comments

the problem was fixed , but what is the diffrence ?
Command substitution has a syntax which is either back-tick OR $(..)
@user3137879: If you assign without $(...) (or backticks), you assign a string literal to LINE. Command substitution means that the string enclosed in $(...) is interpreted as a command and executed, and its result is then assigned to LINE.
Thanks @mklement0 for your edit and explanation. OP: Sorry I was on phone with my client so couldn't explain it properly. See this link for ore details: en.wikipedia.org/wiki/Command_substitution
1

I think you need to put the call to the tr command with backquotes:

LINE=`tr '\n' ' ' < $FILENAME`

1 Comment

Works, but use of backquotes is deprecated in favor of the $(...) syntax - the latter has the advantage that it can be nested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.