Score:0

How to create a directory with current date without linebreak

sd flag

Moin!

I'm using Ubuntu 22.04. I try to create a directory with a shell script. I searched already for solutions, but all what I found creates a folder with a line break at the end. I tried this:

sudo mkdir /data/Backup/Subversion/Backup_$(date +"%Y_%d_%m")

and also this:

_Dir=Backup_$(date +"%Y_%d_%m")
sudo mkdir /data/Backup/Subversion/$_Dir

Another way I found with printf was also not working. When I just enter the following, I get an output with out a line break

printf `date "+%d.%m.%Y"`

But when I combined it, I just get a directory without a date (Backup_)

sudo mkdir /data/Backup/Subversion/Backup_$printf `date "+%d.%m.%Y"`

How can I remove the line break from the directory name? Thanks for feedback!

Martin

hr flag
How exactly did you determine that there is a line break at the end?
Raffa avatar
jp flag
I agree with @steeldriver ... There can't be probably almost certainly a newline character at the end of the directory name from your first example ... And for the printf one you'll need a command substitution within another like `$(printf $(date "+%d.%m.%Y"))` ... But, the visual you got from running that will be lost in that syntax ... So what you see from `printf `date "+%d.%m.%Y"`` is irrelevant to what's returned from the command substitution ... sort of.
hr flag
... I suspect what they may be seeing is actually a *carriage return* `$'\r'` - and it's because they're doing this in a script that was created or saved with Windows line endings
Raffa avatar
jp flag
... And you suspect right @steeldriver :-)
Score:0
sd flag

I assume (sorry) that it is a line break based on what I saw, see screenshots:

  • of my script
  • shell out put after command "ls -l" and
  • the view of the Files window.

I added one screenshot of all 3 screenshots. It's my first post, I don't know how it works. The script was written on Ubuntu, I used Notepad++ (with wine), may be this is the problem? I just tried it with standard text editor from Ubuntu, this works. It seems I should waste the Notepad++ on linux.

enter image description here

martin avatar
sd flag
Thanks steeldriver and also thanks to Raffa, I corrected the syntax of the substitution with printf and this works now also
hr flag
It's fine to use notepad++, just make sure you set the line endings appropriately. See for example [EOL conversion in notepad ++](https://stackoverflow.com/questions/16239551/eol-conversion-in-notepad#16244079)
martin avatar
sd flag
Thanks, I changed it in notepad++
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.