官术网_书友最值得收藏!

  • Bash Cookbook
  • Ron Brash Ganesh Naik
  • 524字
  • 2021-07-23 19:17:37

Formatting your data/output using echo and printf

Sometimes, finding the string or the exact data you are looking for is the easy part of your task, but formatting the output data is tricky. For example, here are some situations that have subtle elements that need to be altered:

  • Echoing output without the newline terminator (\n)
  • Echoing raw hexadecimal (hex) data
  • Printing raw hexadecimal values and printable ASCII characters
  • Concatenating strings
  • Escaping specific characters
  • Aligning text
  • Printing horizontal rules

In addition to tricks, we can also print values to the screen that are also floats as well (in addition to the recipe for math). Wait, what is a hexadecimal number? Yes, another type of data or at least a representation exists. To understand what hexadecimals are, we first need to remember that computers use binary, which consists of 1s and 0s (ones and zeros). However, binary is not very friendly to us humans (we use the decimal format when looking at numbers typically), so other representations are sometimes needed, one of which is called hexadecimal. As you have probably guessed, it is base 16 so it looks like 0x0 to 0xF (0x0,0x1, ... ,0x9,0xA,0xB, ..., 0XF). Here is an example:

$ echo -en '\xF0\x9F\x92\x80\n'

$ printf '\xF0\x9F\x92\x80\n'

In the preceding example, both printf and echo can be used to print raw hex and Unicode characters. Using a Unicode reference, I found the UTF-8 encoding for the skull character (F0 9F 92 80) and then formatted it using \xFF. Notice where FF is; it is in each byte.

What can you do knowing about "raw hex" values? Well, you can send characters that the shell can interpret differently, or you can print neat things! See  unicode-table.com for more detail.

Wait, another term called a byte? Yes, and here is another one called a bit. A bit is the form because it refers to either a 0 or 1, but a byte is 8 bits (one byte is made up of eight bits! Get it?).

As a side note, depending on the platform or measurement be aware that 1 kilobyte or KB can mean 1,024 Bytes (B) or on many marketing datasheets, 1 KB= 1,000 B. Furthermore, when you see Kb it does NOT mean kilobytes. It means Kilo  bits!
Again, being aware of computational basics such as types of data and conversion between basic data forms is a very useful tool to have in your skill set. It might even be in a job interview or two!

However, we are getting a bit ahead of ourselveswhat is echo and printf? Both are commands which you may have seen earlier in this cookbook that allow you to output the contents of variables and more to the console or even to a file. Echo is far more "straightforward", but printf can provide the same and more functionality using C style parameters. In fact, one of the main features of printf over echo is that printf can format characters, pad them, and even align them.

Alright; let's get to work.

主站蜘蛛池模板: 绥阳县| 罗平县| 嵊州市| 炎陵县| 沈丘县| 吉木萨尔县| 千阳县| 尉氏县| 泾源县| 南宫市| 临沭县| 昌都县| 兰西县| 卫辉市| 三门峡市| 浪卡子县| 旅游| 辽阳县| 梅州市| 荥经县| 巴南区| 大方县| 合江县| 徐闻县| 贵阳市| 麻江县| 聊城市| 遵义县| 宁南县| 屏山县| 奉新县| 竹山县| 葫芦岛市| 怀来县| 景德镇市| 云安县| 什邡市| 兴安县| 兰溪市| 墨竹工卡县| 临高县|