Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the newline after shell command


I have here the code:

#/bin/sh

cd ~/Desktop/tmp
date "+%m%d%y_%H%M%S_" | xargs -0 mkdir;

This will create a directory with the current date.
The problem here is that the date command will return a date with a newline character at the end.
After mkdir, the folder created will include a newline.
Anybody know how to go about this? I need a folder name without the newline character.
Thanks.

like image 634
Neilvert Noval Avatar asked Jun 16 '26 20:06

Neilvert Noval


2 Answers

why do you need to pass it to xargs? Don't do the unnecessary

mkdir $(date "+%m%d%y_%H%M%S_")
like image 151
ghostdog74 Avatar answered Jun 19 '26 10:06

ghostdog74


The reason that the newline was included, is that you used the -0 option, which makes xargs use null byte (ASCII 000) as a word boundary. Newlines are then included as part of the words. Dropping -0 makes xargs use whitespace (including newline) as boundary, thus chopping it off.

However, use ghostdog74's solution, it's simpler.

like image 34
maxelost Avatar answered Jun 19 '26 11:06

maxelost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!