Can someone please tell me how I can create a file in Shell that when I run CAT on it, it returns Z followed by a line break? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can someone please tell me how I can create a file in Shell that when I run CAT on it, it returns Z followed by a line break?

1st Oct 2019, 4:50 PM
Nan0brS
4 Answers
+ 4
Schindlabua I think on most machines echo command like this wouldn't work but I m curious which shell you are using? - If we wish to pass escaped characters in echo command then we ll have to use it with -e option ( enable interpretation of backslash characters). $echo -e "Z\n" > myfile.txt - Another option using printf command on bash shell. $printf "Z\n" > myfile.txt - We can also do $cat > myfile.txt # your input with new lines here and then to save your input press "ctrl + d" More on this here https://forum.ivorde.com/how-to-echo-newline-in-linux-unix-shell-t14281.html
1st Oct 2019, 7:59 PM
Morpheus
Morpheus - avatar
+ 3
Ah, fair. I should have tested it!
1st Oct 2019, 8:03 PM
Schindlabua
Schindlabua - avatar
+ 3
No probs 🙂, I had this issue recently in one of our automation scripts so it was fresh in my mind. Btw just sharing a slightly off topic fact when I was goin through $man echo I saw -n option that excludes the trailing new line for echo command and It was a revelation for me. Before knowing this it was such a pain and I had to replace \n everytime during chaining operations after echo. I didn't knew about that option. so glad I know it now because if this Q&A thread.
1st Oct 2019, 8:16 PM
Morpheus
Morpheus - avatar
0
echo "Z\n" > myfile.txt
1st Oct 2019, 5:10 PM
Schindlabua
Schindlabua - avatar