Bash 101 Part 2: Concatenation, Capturing command output and Case statements


This is part 2 in a series about Bash. My primary purpose is to document what I've learned over the last week. Today I'm focusing on:

  • String concatenation
  • Capturing executed command output
  • Basic Case statements

Notes:

 

The first thing I want to cover is string contatenation. In Bash, this is a relatively straightforward operation.

Example:

#!/bin/bash
AA="Concate"
BB="nation test"
CC=$AA$BB
echo "$CC"

This script defines 2 variables (AA and BB), concatenates them into a third (CC) then spits out the combined output. There are a few to note:

  • NO spaces appear on either side of the equals signs. This is required for assignment
  • Placing two variables next to each other on the same line effectively concatenates them. You store the concatenated output in another variable for later use
  • Double quotes around the CC variable ensure that what is contained shows up as a string (Not required in this case, but comes in handy)
  • In case I forgot to mention it, Bash variables are assigned without the dollar-sign prefix and used WITH the dollar sign prefix.

 

Executing commands and capturing their StandardOutput:

Capturing executable output is a must-have in any scripting language. It took me a little while to feel comfortable with the bash approach:

#!/bin/bash
OUTPUT=$(eval ls\ -la)

echo "$OUTPUT"

Here is a walk through of what happens here:

  • OUTPUT= assigns the output of the command to the OUTPUT variable
  • $(eval ls\ -la) executes the ls -la command.
    Note that the space between ls and la is escaped. This is not required here, but in other places space escaping becomes essential
    Note that the general syntax is $(eval COMMAND ARGS)
  • echo "$OUTPUT" prints the commands output to the screen.
    Note that in this case the Double-quotes around $OUTPUT are required for proper output string formatting. Without the double quotes, tabs and newlines are ignored and the text comes out a garbled mess.

You can alter the command above to print the number of characters in the returned variable rather than the contents:

#!/bin/bash
OUTPUT=$(eval ls\ -la)

echo ${#OUTPUT}

The line ${#OUTPUT} will print the number of characters.
Note: The general use is ${#VARIABLE_NAME_WITHOUT_DOLLAR_SIGN}.

 

Using the Case Statement (Known as a switch statement in some languages):

If-Then-Else statements are great, but they are no substitute for case statements for certain operations. Here is a sample case statement in bash. It doesn't do anything aside from illustrate how case statements work:

#!/bin/bash
CC=concatenatio

case $CC in

  concatenation)
     echo "Concatenation is cool!" ;;
  not)
     echo "What is concatenation?" ;;
  *)
     echo "What happened here?!?" ;;
esac

In this case with the given input you should expect to see "What happened here?!?" appear on the console screen. Now for an explanation of the syntax:

 

  • CC=concatenatio defines the CC variable with a string value
  • case $CC in sets up the beginnings of the case statement. Basically we're saying to pull the possible cases from the CC variable
  • concatenation) defines the beginning of a case. The value before the closing parenthesis is the value of the case that needs to be met before it is executed
  • echo "Concatenation is cool!" ;; Everything up to the double smi-colon is what gets executed when the concatenation) case is chosen
  • not)
         echo "What is concatenation?" ;; This is another possible case in the case statement.
  • *) This is the Default Case. If none of the other cases get executed, this one will be executed (which is what happens in our sample script above).
  • echo "What happened here?!?" ;; This is what gets executed when Default case is used.
  • esac  This command closes off the case statement. it is 'case' spelled backwards.

 

All told, these 3 functions make for powerful scripting opportunities! You could setup a case statement which is based the output of a command. When I cover Regex in bash at a later point you will see more powerful opportunities!