I was writing a simple bash script for a small task.
echo starting
URL="10.10.0.1/api/monitoring/traffic-statistics"
curl -b cookie $URL
When I executed it using bash script.sh
, it just prints curl -b cookie pi/monitoring/traffic-statistics
. It's as if the file said echo curl -b cookie $URL
and somehow URL got corrupt.
When I type cat script.sh
, instead of listing the file contents, it just displays a part of one line in the script.
Note: I was editing the file in vim, and it was displaying it without any issue. Nano editor also showed the same content. But when it comes to bash, it messes up the order, either when executing the file or just dumping the contents. This is the hex dump. (xxd script.sh
)
00000000: 6563 686f 2073 7461 7274 696e 670d 5552 echo starting.UR
00000010: 4c3d 2231 302e 3130 2e30 2e31 2f61 7069 L="10.10.0.1/api
00000020: 2f6d 6f6e 6974 6f72 696e 672f 7472 6166 /monitoring/traf
00000030: 6669 632d 7374 6174 6973 7469 6373 220d fic-statistics".
00000040: 6375 726c 202d 6220 636f 6f6b 6965 2024 curl -b cookie $
00000050: 5552 4c0d URL.
I can't seem to find any stray \r
character either.
What did I do wrong here and how do I produce a proper file that bash will accept?
Best Answer
You misconfigured your vimrc and a carriage return (CR or 0x0d) instead of a line feed (LF or 0x0a) is entered/read.
So modify the vimrc back to the standard behavior.
The missing shebang (e.g.
#!/bin/bash
) at the beginning is not the cause of the failing script. I would include it though - just for redundancy.