How to do it...

Let's begin our activity:

  1. Open a new terminal and a new file with your preferred editor. Add in the following contents into the new script and save it as builtin-str.sh:
#!/bin/bash# Let's play with variable arrays first using Bash's equivalent of substrSTR="1234567890asdfghjkl"echo "first character ${STR:0:1}"echo "first three characters ${STR:0:3}"echo "third character onwards ${STR: 3}"echo "forth to sixth character ${STR: 3: 3}"echo "last character ${STR: -1}"# Next, can we compare the alphabeticalness of strings?STR2="abc"STR3="bcd"STR4="Bcd"if [[ $STR2 < $STR3 ]]; then echo "STR2 is less than STR3"else echo "STR3 is greater than STR2"fi# Does case have an effect? Yes, b is less than Bif [[ $STR3 < $STR4 ]]; then echo ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.