# 获取字符串最后几个字母

tail指令提供了获取文件最后几行数据的功能，例如 `tail -10 file` 则显示文件最后10行。

实际上，`tail`加上参数`-c`就可以按照字符来获取最后的数据

```bash
echo "hello" | tail -c 5
ello
```

注意：获取最后4个字符使用的是`-c 5`是因为`echo`指令显示的字符串组后有隐含换行`\n`。如果要避免机上新行（newline character），则使用`echo -n`。

另外，shell支持切片方法：

```bash
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
```

其实比较简明的是结合使用`head -c`和`tail -c`，例如要截取`hello`的倒数第四个字符`e`:

```bash
echo -n "hello" | tail -c 4 | head -c 1
```

## 参考

* [How to get the last character of a string in a shell?](https://stackoverflow.com/questions/17542892/how-to-get-the-last-character-of-a-string-in-a-shell)
* [BASH : Get the last 4 characters of output from Standard Out, Inline](https://stackoverflow.com/questions/9219964/bash-get-the-last-4-characters-of-output-from-standard-out-inline)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/shell/bash/get_last_character_of_string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
