Please enable JavaScript eh!

 ⌘ Web Mechanic ⌘ 

Bash Scripting


Strings

String manipulation is large part of programming, so we need to discuss how we do this in bash scripting.

Remember, bash is just the shell, and has a lot of tools available for string mangling, but there are other tools available as well such as grep, cut, awk and sed.

We will talk about them here as well as the basics.

Strap in for the ride!

Recall that strings are simply arrays of characters, and can be handled as such.

Length

How long is that string? How long is the coastline of England?

No, we're not going to be talking fractals here, but the actual length (number of characters) of a string.

There are many reasons why we would need to know this:

  • a field length in a database requires it
  • your password needs to be >=x characters long
  • just curious
  • you need the right size to fit your page width

    Simple enough:

    var="A long string in this variable"
    echo $var
    varLength="${#var}"
    echo "That string has " $varLength " characters"
    
    returns ...
    A long string in this variable
    That string has  30  characters
    
    varE="-->Here we have some ₿ ℃ ∴ ⌘ 🛠️ ☕️<--"
    echo $varE
    varElength="${#varE}"
    echo "That string has "$varElength" characters"
    
    returns ...
    -->Here we have some ₿ ℃ ∴ ⌘ 🛠️ ☕️<--
    That string has 37 characters
    

    And this one ..

    varF="-->and some ❌ ⣻ ⭕ ⻨ ⿕ 㕕<--"
    varFlength="${#varF}"
    echo "\$varF: $varF"
    echo "\$varF has " $varFlength "characters"
    
    returns
    $varF: -->and some ❌ ⣻ ⭕ ⻨ ⿕ 㕕<--
    $varF has 26 characters
    

    That also works with printf:

    printf "%s\n" "$varF"
    -->and some ❌ ⣻ ⭕ ⻨ ⿕ 㕕<--
    

    Reverse

    Another task you may need to do is to reverse a string. Bash can do that, but there are alternatives.

    Note that reversing may have 2 meanings:

  • reversing word order
  • reversing character order

    For example 'Reversing this string' might mean:

  • string this Reversing
  • or
  • gnirts siht gnisreveR
  • We will provide both solutions. First character order:

    input="Life is a fractal in Hilbert space"
    i=${#input}
    echo "in : -->$input<--"
    while [ $i -ge 0 ]
    do
        revstr=$revstr${input:$i:1}
        i=$((i-1))
    done
    echo "out: -->$revstr<--"
    

    in : -->Life is a fractal in Hilbert space<--
    out: -->ecaps trebliH ni latcarf a si efiL<--

    Now 2 ways to do reverse word order.

    input=($input) # put each word into array
    inputlength=${#input[@]}
    inputlength=`expr $inputlength - 1`
    while [ $inputlength -ge 0 ]
    do
    	echo -n ${input[inputlength]}
    	echo -n " "
    	inputlength=`expr $inputlength - 1`
    done
    echo;echo
    
    input="Life is a fractal in Hilbert space" # put back into a string
    echo "Using awk ..."
    echo "\$input: -->$input<--"
    echo $input | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }'
    

    space Hilbert in fractal a is Life
    Using awk ...
    $input: -->Life is a fractal in Hilbert space<--
    space Hilbert in fractal a is Life
    Finding / Matching