如何获取Bash变量中的第一个字母? [英] How to obtain the first letter in a Bash variable?
本文介绍了如何获取Bash变量中的第一个字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Bash变量$word
,有时它是一个单词或句子,例如:
I have a Bash variable, $word
, which is sometimes a word or sentence, e.g.:
word="tiger"
或者:
word="This is a sentence."
如何创建一个新的Bash变量,该变量仅等于该变量中找到的第一个字母?例如,上面是:
How can I make a new Bash variable which is equal to only the first letter found in the variable? E.g., the above would be:
echo $firstletter
t
或者:
echo $firstletter
T
推荐答案
initial="$(echo $word | head -c 1)"
每次在问题描述中说第一"时,head
是可能的解决方案.
Every time you say "first" in your problem description, head
is a likely solution.
这篇关于如何获取Bash变量中的第一个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文