Is bash a programming language?

There is no perfect definition of what a programming language really is but you can say that every language that is Turing-complete is a programming language in the sense of that every thinkable program can theoretically be written in it (even if it may be awkward to do so and even if it would be horribly slow to run). And Bash is Turing-complete, so there is nothing that could not be programmed in Bash.

The problem with Bash, shells in general is, that it lacks a lot of base functionality, thus when writing scripts for them, you often in fact call external programs to perform the desired work. But that's only taking a shortcut. E.g. if you'd need floating point functionality in a shell, you could actually implement it. It would be possible to write a full IEEE 754 standard implementation in everything that is Turing-complete. In practice such an implementation would be huge, require tons of memory and be horribly slow, so one better calls bc for that. But even implementing bc entirely in bash would be possible.

Here's a bash script I've once written that draws a Mandelbrot set to console. You better be prepared to get some cups of coffee if you want to see the final result, it's going to be a very long night:

#!/bin/bash

BAILOUT=16
MAX_ITERATIONS=1000

function iterate {
    # $1 is x
    # $2 is y
    local zi=0
    local zr=0
    local i=0

    local cr
    cr=$(printf "%s\n" "scale=16; $2 - 0.5" | bc)

    while true
    do
        local temp
        local zr2
        local zi2
        i=$((i + 1))
        zr2=$(printf "%s\n" "scale=16; ($zr * $zr) - ($zi * $zi) + $cr" | bc)
        zi2=$(printf "%s\n" "scale=16; (($zr * $zi) * 2) + $1" | bc)
        temp=$(printf "%s\n" "(($zi * $zi) + ($zr * $zr)) > $BAILOUT" | bc)

        if ((temp == 1))
        then
            return "$i"
        fi

        if ((i > MAX_ITERATIONS))
        then
            return 0
        fi

        zr="$zr2"
        zi="$zi2"
    done
}

function mandelbrot {
    local y
    for ((y = -39; y < 39; y++))
    do
        printf "\n"
        local x
        for ((x = -39; x < 39; x++))
        do
            local xi
            local yi
            local ires
            xi=$(printf "%s\n" "scale=16; $x / 40.0" | bc)
            yi=$(printf "%s\n" "scale=16; $y / 40.0" | bc)
            iterate "$xi" "$yi"
            ires=$?

            if ((ires == 0))
            then
                printf "*"
            else
                printf " "
            fi
        done
    done
    printf "\n"
}

mandelbrot

For those who cannot wait that long, the result should look like this:

                                       *                                      
                                       *                                      
                                       *                                      
                                       *                                      
                                       *                                      
                                      ***                                     
                                     *****                                    
                                     *****                                    
                                      ***                                     
                                       *                                      
                                   *********                                  
                                 *************                                
                                ***************                               
                             *********************                            
                             *********************                            
                              *******************                             
                              *******************                             
                              *******************                             
                              *******************                             
                            ***********************                           
                              *******************                             
                              *******************                             
                             *********************                            
                              *******************                             
                              *******************                             
                               *****************                              
                                ***************                               
                                 *************                                
                                   *********                                  
                                       *                                      
                                ***************                               
                            ***********************                           
                         * ************************* *                        
                         *****************************                        
                      * ******************************* *                     
                       *********************************                      
                      ***********************************                     
                    ***************************************                   
               *** ***************************************** ***              
               *************************************************              
                ***********************************************               
                 *********************************************                
                 *********************************************                
                ***********************************************               
                ***********************************************               
              ***************************************************             
               *************************************************              
               *************************************************              
              ***************************************************             
              ***************************************************             
         *    ***************************************************    *        
       *****  ***************************************************  *****      
       ****** *************************************************** ******      
      ******* *************************************************** *******     
    ***********************************************************************   
    ********* *************************************************** *********   
       ****** *************************************************** ******      
       *****  ***************************************************  *****      
              ***************************************************             
              ***************************************************             
              ***************************************************             
              ***************************************************             
               *************************************************              
               *************************************************              
              ***************************************************             
                ***********************************************               
                ***********************************************               
                  *******************************************                 
                   *****************************************                  
                 *********************************************                
                **** ****************** ****************** ****               
                 ***  ****************   ****************  ***                
                  *    **************     **************    *                 
                         ***********       ***********                        
                         **  *****           *****  **                        
                          *   *                 *   *      

It shall resemble this kind kind of thing turned by 90 degree (and a bit squeezed):

Mandelbrot set


We can say that yes, it is a programming language.

According to man bash, Bash is a "sh-compatible command language". Then, we can say a "command language" is "a programming language through which a user communicates with the operating system or an application".

From man bash:

DESCRIPTION

Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incorporates useful features from the Korn and C shells (ksh and csh).

http://www.gnu.org/software/bash/

Bash is the GNU Project's shell. Bash is the Bourne Again SHell. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use. In addition, most sh scripts can be run by Bash without modification.

And a UNIX shell is... http://en.wikipedia.org/wiki/Unix_shell

A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems. Users direct the operation of the computer by entering commands as text for a command line interpreter to execute, or by creating text scripts of one or more such commands. Users typically interact with a Unix shell using a terminal emulator, however, direct operation via serial hardware connections, or networking session, are common for server systems.


Bash most certainly is a programming language, one that specialises in the unix/linux shell scripting. It's turing complete so you could (theoretically) write any program in Bash.

Tags:

Bash