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?
Use double quotes:
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:
aandb. With quotes, bash only passes one argument and does not discard the newline.