简体   繁体   English

在shell命令之后删除换行符

[英]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. 这里的问题是date命令将在结尾返回一个带换行符的日期。
After mkdir , the folder created will include a newline. mkdir之后,创建的文件夹将包含换行符。
Anybody know how to go about this? 谁知道怎么回事? I need a folder name without the newline character. 我需要一个没有换行符的文件夹名称。
Thanks. 谢谢。

why do you need to pass it to xargs ? 为什么你需要将它传递给xargs Don't do the unnecessary 不要做不必要的事

mkdir $(date "+%m%d%y_%H%M%S_")

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. 包含换行符的原因是您使用了-0选项,这使得xargs使用空字节(ASCII 000)作为单词边界。 Newlines are then included as part of the words. 然后将换行符作为单词的一部分。 Dropping -0 makes xargs use whitespace (including newline) as boundary, thus chopping it off. 删除-0会使xargs使用空格(包括换行符)作为边界,从而将其删除。

However, use ghostdog74's solution, it's simpler. 但是,使用ghostdog74的解决方案,它更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM