O'Reilly logo

Mastering Bash by Giorgio Zarrelli

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Pattern matching against variables

We have a few ways to fiddle with variables, and some of these have a really interesting use in scripts, as we will see later on in this book. Let's briefly recap what we can do with variables and how to do it, but remember we are dealing with values that are returned, not assigned back to the variable:

${#variable)  

It gives us the length of the variable, or if it is an array, the length of the first element of an array. Here is an example:

zarrelli:~$ my_variable=thisisaverylongvaluezarrelli:~$ echo ${#my_variable}20  

And indeed thisisaverylongvalue is made up of  20 characters. Now, let us see an example with arrays:

zarrelli:~$ fruit=(apple pear banana)  

Here, we instantiated an array with three elements ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required