2

I am trying to display certain details using a bash script but the output of the bash script differs from the output of the terminal.

Terminal Output:

ubuntu@ubuntu:~/ubin$ cat schedule.text  | grep 09/06/12
Sat 09/06/12 Russia           00:15 Czech Republic   A
Sat 09/06/12 Netherlands      21:30 Denmark          B
ubuntu@ubuntu:~/ubin$ 

Bash Script Output:

ubuntu@ubuntu:~/ubin$ bash fixture.sh 
Sat 09/06/12 Russia 00:15 Czech Republic A Sat 09/06/12 Netherlands 21:30 Denmark B
ubuntu@ubuntu:~/ubin$ 

As you can see the output of the bash script differs from the output of the terminal. My bash script output has everything in a single line.

fixture.sh:

A=$(date +%d/%m/%y) #get today's date in dd/mm/yy fmt
fixture=$(cat /home/ubuntu/ubin/schedule.text | grep $A)
echo $fixture

So, my question is how do I make my bash script output similar to the terminal output?

2 Answers 2

2

Use double quotes:

echo "$fixture"

When the variable fixture has embedded newlines and is unquoted, bash splits it into different arguments to echo. To simplify, suppose fixture is the string "a\nb". Without quotes, bash passes two arguments to echo: a and b. With quotes, bash only passes one argument and does not discard the newline.

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

2 Comments

ok double-quotes did the trick. Can you provide a little detail or point me to the correct documentation of double-quote trick.
Thanks for the explanation.Will accept the answer in 6 minutes.
1

You don't need echo, or cat:

A=$(date +%d/%m/%y) #get today's date in dd/mm/yy fmt
grep $A /home/ubuntu/ubin/schedule.text

Or, if you prefer one-liner:

grep $(date +%d/%m/%y) /home/ubuntu/ubin/schedule.text

1 Comment

oh I didn't knew that. Thanks

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.