1

quick question, how do I write a shell script that prompts users to enter let say 5 lines and it will stores those lines into a .txt file for example,

Today is hot 
Today is code 
Today is chilly
Today is windy
Today is sunny

not like

Today is hot  Today is code  Today is chilly Today is windy today is sunny

Thanks for your help

3
  • 1
    It depends on the shell. Commented Oct 4, 2010 at 2:52
  • You are aware that that's four lines, aren't you, and that 'code' is not a adjective? :-) Commented Oct 4, 2010 at 3:02
  • If you're getting everything on one line when you do an echo it may be because you're not quoting a variable. Since you don't show any code, it's impossible to say for sure, but that scenario fits the two examples you show in your question. Commented Oct 4, 2010 at 4:02

2 Answers 2

2

In bash you can do:

for i in {1..5}
do
  read line
  echo $line >> file.txt
done
Sign up to request clarification or add additional context in comments.

1 Comment

Or ... read -r line; echo "$line"; done > file.text - add -r to accept backslashes literally, quote the variable to preserve whitespace and do all your output at once.
0

Does this count?

sed 5q > file.txt

Comments

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.