I have the following file
linux$ cat test.txt toto titi tete tata
Saving the cat output into a variable will discard the newlines
linux$ msgs=`cat test.txt` linux$ echo $msgs toto titi tete tata
How to keep the output containing the newlines in the variables?
To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"
The cat (short for “concatenate“) command is one of the most frequently used commands in Linux/Unix-like operating systems. cat command allows us to create single or multiple files, view content of a file, concatenate files and redirect output in terminal or files.
There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way.
The shell is splitting the msgs
variable so echo
get multiple parameters. You need to quote your variable to prevent this to happen:
echo "$msgs"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With