0

I am using a bash script where I must store the last n-2 lines of a file in a variable. The command I use for this is

last_n2_lines=`tail -n+3 $file`

Now whenever i echo the $last_n2_lines variable, I get all the lines of my file in a single line. How can I get the same formatting as I had in my original file. I tried printf but it just prints the first field.

1 Answer 1

2

Try this:

echo "$last_n2_lines"

echo doesn't see the original value in your variant, because it's broken down to argv array.

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

4 Comments

& to add, it's not echo's fault. It's the way shell expands the variables. with double quotes, echo receives ONE argument, which holds entire data.
does echo do anything funny when the variable contains a ^L character.
it does not. When working interactive, bash can do something funny (^ is somewhat special for history substitution, see histchars in man bash), but not in a script.
And if you mean Control+L (not separate ^+L), it shouldn't be special, neither for bash nor for echo.

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.