# 补全字符串

在bash中，有时候我们需要把一个字符串补全到一定字符长度，例如，员工工号要求6位数字，但是早期员工的工号较短。为了能够统一采用6位数字，我们需要补全字符串。

解决的方法是采用 `printf` 程序，例如

```bash
printf %-10s AABB | tr ' ' X
```

输出就是 `AABBXXXXXX`

```bash
printf %10s AABB | tr ' ' X
```

输出就是`XXXXXXAABB`

另外，还有一种比较巧妙的方法：

```bash
# length of maximum padding
padding="......................................"

printf "==== TABLE OF CONTENTS ===========================\n"

# print first line
title="1) Chapter one - the intro"
printf "%s%s %s\n" "$title" "${padding:${#title}}" "Page 1"

# print second line
title="2) Chapter two - summary"
printf "%s%s %s\n" "$title" "${padding:${#title}}" "Page 4"
```

> `${#title}` 是获得 `title` 字符串的长度

输出结果就是

```
==== TABLE OF CONTENTS ===========================
1) Chapter one - the intro............ Page 1
2) Chapter two - summary.............. Page 4
```

## 参考

* [Pad a string to a certain length with a chosen character (or hexcode) in Bash?](https://stackoverflow.com/questions/50604111/pad-a-string-to-a-certain-length-with-a-chosen-character-or-hexcode-in-bash)
* [Bash: using printf to display fixed-width padded string](https://fabianlee.org/2021/06/09/bash-using-printf-to-display-fixed-width-padded-string/)


---

# 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/pad_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.
