0

file.sh

#!/bin/bash
fileName="Screenshot_$(TZ=GMT-3 date +%Y%m%d_%H%M%S).png"
echo "|$fileName|"

Terminal at Ubuntu 19.04:

> bash file.sh
|Screenshot_20190521_104141.png
|

I want to understand why a new-line is added to the variable at end?

David Zagi
  • 1,383
  • 2
  • 10
  • 18
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
  • Your `filename` variable _most_ likely contains a DOS CRLF instead of Unix's LF alone. You can easily recreate this by something like `fileName=$'AnyName.png\r\n'` – Inian May 21 '19 at 09:33
  • Remove it as `"${fileName//$'\r\n'/}"` – Inian May 21 '19 at 09:35
  • 1
    You are probably working on a DOS editor like Notepad++, which default encodes CRLF endings. Either change the setting to use Unix style endings or clean up the script by running `dos2unix ` – Inian May 21 '19 at 09:46
  • @Inian Thanks, works! Didn't know about that – NickUnuchek May 21 '19 at 09:50

0 Answers0