+ 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?
4 Respostas
+ 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
+ 3
Ah, fair. I should have tested it!
+ 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.
0
echo "Z\n" > myfile.txt