So far, we have seen inkling of the power of commands available in Linux, and some of these are among the most powerful: sed
and grep
. However, while we can easily use these commands together, sed by itself or even using another very useful command called awk
, we can leverage Bash itself to shave time and reduce external dependencies in a portable way!
So, how can we do this? Let's begin with a few examples using this Bash syntax:
#!/bin/bash # Index zero of VARIABLE is the char 'M' & is 14 bytes long VARIABLE="My test string" # ${VARIABLE:startingPosition:optionalLength} echo ${VARIABLE:3:4}
In the preceding example, we can see a special way of calling special substring functionality using ${...}
, where VARIABLE
is a string variable within your script (or even global), and then the following variable is the :
. After the :
, there is the startingPosition
parameter (remember that strings are just arrays of characters and...