string 如何在shell中获取字符串的最后一个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17542892/
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 02:00:43  来源:igfitidea点击:

How to get the last character of a string in a shell?

stringbashshell

提问by thinker3

I have written the following lines to get the last character of a string:

我写了以下几行来获取字符串的最后一个字符:

str=
i=$((${#str}-1))
echo ${str:$i:1}

It works for abcd/:

它适用于abcd/

$ bash last_ch.sh abcd/
/

It does not work for abcd*:

不适用于abcd*

$ bash last_ch.sh abcd*
array.sh assign.sh date.sh dict.sh full_path.sh last_ch.sh

It lists the files in the current folder.

列出了当前文件夹中的文件

采纳答案by perreal

That's one of the reasons why you need to quote your variables:

这就是您需要引用变量的原因之一:

echo "${str:$i:1}"

Otherwise, bash expands the variable and in this case does globbing before printing out. It is also better to quote the parameter to the script (in case you have a matching filename):

否则,bash 会扩展变量,并且在这种情况下会在打印之前进行 globbing。最好将参数引用到脚本中(以防您有匹配的文件名):

sh lash_ch.sh 'abcde*'

Also see the order of expansions in the bash reference manual. Variables are expanded before the filename expansion.

另请参阅bash 参考手册中的扩展顺序。在文件名扩展之前扩展变量。

To get the last character you should just use -1as the index since the negative indices count from the end of the string:

要获取最后一个字符,您应该将其-1用作索引,因为负索引从字符串的末尾开始计数:

echo "${str: -1}"

The space after the colon (:) is required.

冒号 ( :)后的空格是必需的。

This approach will not work without the space.

如果没有空间,这种方法将不起作用。

回答by quickshiftin

Per @perreal, quoting variables is important, but because I read this post like 5 times before finding a simpler approach to the question at hand in the comments...

根据@perreal,引用变量很重要,但是因为在评论中找到解决手头问题的更简单方法之前,我阅读了这篇文章 5 次......

str='abcd/'
echo "${str: -1}"

Output: /

输出: /

str='abcd*'
echo "${str: -1}"

Output: *

输出: *

Thanks to everyone who participated in this above; I've appropriately added +1's throughout the thread!

感谢所有参与上述活动的人;我在整个线程中适当地添加了 +1!

回答by user5394399

I know this is a very old thread, but no one mentioned which to me is the cleanest answer:

我知道这是一个非常古老的线程,但没有人提到哪个对我来说是最干净的答案:

echo -n $str | tail -c 1

Note the -nis just so the echo doesn't include a newline at the end.

请注意,这-n只是使 echo 在末尾不包含换行符。

回答by mark_infinite

Try:

尝试:

"${str:$((${#str}-1)):1}"

For e.g.:

例如:

someone@mypc:~$ str="A random string*"; echo "$str"
A random string*
someone@mypc:~$ echo "${str:$((${#str}-1)):1}"
*
someone@mypc:~$ echo "${str:$((${#str}-2)):1}"
g

回答by phep

Every answer so far implies the word "shell" in the question equates to Bash.

到目前为止,每个答案都暗示问题中的“shell”一词等同于 Bash。

This is how one could do that in a standard Bourne shell:

这是在标准的 Bourne shell 中如何做到这一点的:

printf $str | tail -c 1

回答by mr.baby123

another solution using awk script:

使用 awk 脚本的另一个解决方案:

last 1 char:

最后 1 个字符:

echo $str | awk '{print substr(
echo $str | awk '{print substr(
${str:${#str}-1:1}
,length-5,5)}'
,length,1)}'

last 5 chars:

最后 5 个字符:

echo "${str:${#str}-1:1}"

回答by Eduardo Cuomo

Single line:

单线:

echo $str | cut -c $((${#str}))

Now:

现在:

##代码##

回答by pradeep

##代码##

is a good approach

是个好方法