I try to parse a plain text config file A.conf with the following content:

bitrate 500000
use_can 1

I want to use the number behind bitrate to setup a CAN-interface with the appropriate bitrate. Here is the corresponding part of my shell script:

bitrate="$(cat /home/pi/A.conf | grep 'bitrate' | awk '{print $2}')"
sudo /sbin/ip link set can0 up type can bitrate $bitrate

It works fine, if there is at least one empty space character after the number 500000, but not if it is immediately followed by a newline. In that case I get this error output:

" is wrong: invalid "bitrate" value

What is the reason for this behaviour and how can I fix it? I don't want to rely on the fact that each config file needs empty space characters before each new line.


1 Answer 1


Your input file apparently contains a DOS carriage return (ctrl-M, octal \015). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.

bitrate="$(tr -d '\015' < /home/pi/A.conf | awk '/bitrate/ {print $2}')"

Notice also how this does away with the useless cat and the useless grep.

  • 2
    Without tr: bitrate="$(</home/pi/A.conf awk -F '[ \r]' '/bitrate/ {print $2}')" Aug 13, 2018 at 10:13
  • 2
    Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
    – tripleee
    Aug 13, 2018 at 10:19
  • Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
    – oh.dae.su
    Aug 13, 2018 at 10:28
  • It would be more natural imho to set the record separator to DOS style (e.g. -vRS='\r\n') rather than adding \r as a field separator. If you do change FS, I'd recommend making it [ \t\r]+ to preserve the default behavior of splitting on one or more whitespace characters. Aug 13, 2018 at 11:07
  • Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
    – tripleee
    Aug 13, 2018 at 11:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .