如何在 Bash 中检索变量的前 n 个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14992658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:21:08  来源:igfitidea点击:

How to retrieve the first n characters of a variable in Bash?

bash

提问by user2093552

How to retrieve the first 10 characters of a variable with Bash?

如何使用 Bash 检索变量的前 10 个字符?

FOO="qwertzuiopasdfghjklyxcvbnm"

I need to get qwertzuiop.

我需要得到qwertzuiop

回答by P.P

If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"

如果变量是: FOO="qwertzuiopasdfghjklyxcvbnm"

then

然后

 echo ${FOO:0:10}

will give the first 10 characters.

将给出前 10 个字符。

回答by pje

Use the headcommand.

使用head命令。

echo $FOO | head -c 10
=> qwertzuiop