简体   繁体   English

Bash:如何避免行首出现空白?

[英]Bash: How to avoid blank at the beginning of a line?

Given such a command in bash:在 bash 中给出这样的命令:

echo -e "a b "{1..3}" d e\n"
a b 1 d e
 a b 2 d e
 a b 3 d e

the output of line 2... starts with a blank.第 2 行的 output ... 以空白开头。 Why is that?这是为什么? Where does it come from?它从何而来? How can I avoid it?我怎样才能避免它?

I do know how to get rid of it with sed or something else, but I would prefer to avoid it in the first place.我确实知道如何用 sed 或其他东西摆脱它,但我宁愿首先避免它。

I've only mentioned it, together with {..}-Syntax.我只提到了它,连同 {..}-Syntax。 Are there other, similar cases, without it?没有它,还有其他类似的情况吗?

update:更新:

A useful workaround is, to remove the first letter with backspace:一个有用的解决方法是,用退格键删除第一个字母:

echo -e "\ba b "{1..3}" d e\n"

or, as Jared Ng suggests:或者,正如 Jared Ng 所建议的:

echo -e "\ra b "{1..3}" d e\n"

update 2:更新2:

We get rid of leading newlines with:我们摆脱了领先的换行符:

echo -e "\na b "{1..4}" d e" | sed '1d'
echo -e "\na b "{1..4}" d e" | tail -n +2

or trailing:或尾随:

echo -e "\ba b "{1..3}" d e\n" | sed '$d'
echo -e "\ba b "{1..3}" d e\n" | head -n 3
echo -e "\ra b "{1..3}" d e\n"

Fixes it for me.为我修复它。 Output: Output:

a b 1 d e
a b 2 d e
a b 3 d e

( \r is the carriage return -- it brings the caret back to the beginning of the line, preventing the space) \r是回车 - 它将插入符号带回行首,防止空格)

Why does bash do this?为什么bash会这样做? Run echo "abc"{1..3} .运行echo "abc"{1..3} You'll see that bash outputs: abc1 abc2 abc3.您会看到 bash 输出:abc1 abc2 abc3。 Notice the spaces in between?注意到中间的空格了吗? {1..3} expands the expression, delimited by spaces. {1..3}扩展表达式,由空格分隔。 When you use a newline, as in echo "abc"{1..3}"\n" , bash keeps those spaces and simply adds a newline as requested.当您使用换行符时,如echo "abc"{1..3}"\n" ,bash 保留这些空格并根据要求简单地添加换行符。 Those are what show up in the output.这些就是 output 中出现的内容。

The {...} expands to a sequence of space-delimited alternatives, so you have in effect {...}扩展为一系列以空格分隔的替代方案,因此您有效果

echo "a b "1" d e\n" "a b "2" d e\n" "a b "3" d e\n"

The spaces after \n are the spaces you're seeing in the output. \n之后的空格是您在 output 中看到的空格。

You can try to move \n to the beginning of your string, like in您可以尝试将\n移动到字符串的开头,例如

echo -e "\na b "{1..3}" d e"

Update更新

Another method (untested, sorry)另一种方法(未经测试,抱歉)

 printf "%s\n" "a b "{1..3}" d e"

Brace expansion was designed to generate a list of entries to be used as arguments to commands.大括号扩展旨在生成用作 arguments 命令的条目列表。 The generated entries are always space-delimited, hence the extra spaces.生成的条目总是以空格分隔,因此有额外的空格。

To use the {1..3} syntax as is, you'll need to use a command that can treat each generated entry as an independent arg ( echo simply prints them out as it sees it).要按原样使用{1..3}语法,您需要使用可以将每个生成的条目视为独立 arg 的命令( echo只是将它们打印出来)。 A good option here is printf (as suggested by @glenn in the comments above). printf所建议的那样)。

[me@home]$ printf "%s\n" "a b "{1..3}" d e"
a b 1 d e
a b 2 d e
a b 3 d e

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

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