当设置IFS拆就换行,为什么有必要包括退格? [英] When setting IFS to split on newlines, why is it necessary to include a backspace?

查看:104
本文介绍了当设置IFS拆就换行,为什么有必要包括退格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,为什么设置IFS拆就这样换行,当退格是必要的:

I'm curious as to why the backspace is necessary when setting IFS to split on newlines like this:

IFS=$(echo -en "\n\b")

为什么我不能只是用这个(不工作)呢?

Why can I not just use this (which doesn't work) instead?

IFS=$(echo -en "\n")

我即节约Unix行结尾的文件在Linux系统上。我已经转换我的文件用换行到十六进制,绝对只使用0A作为换行符。

I'm on a Linux system that is saving files with Unix line endings. I've converted my file with newlines to hex and it definitely only uses "0a" as the newline character.

我GOOGLE了很多,虽然很多网页文件换行后跟退格的解决方案,没有,我发现解释了为何需要退格。

I've googled a lot and although many pages document the newline followed by backspace solution, none that I have found explain why the backspace is required.

-David。

推荐答案

由于作为bash的手册说有关的命令替换

Because as bash manual says regarding command substitution:

执行的Bash执行命令和命令的标准输出替换命令替换,删除与任何尾随换行符的扩展。

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.

所以,加入 \\ b 您prevent去除的\\ n

So, by adding \b you prevent removal of \n.

一个更清洁的方式做到这一点可能是使用 $''引用,就像这样:

A cleaner way to do this could be to use $'' quoting, like this:

IFS=$'\n'

这篇关于当设置IFS拆就换行,为什么有必要包括退格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆