Is bash a programming language?

Bash

Bash Problem Overview


TL;DR; What are shell scripts? Is it a programming language / is there a programming language we use in shell scripts?


disclaimer: a bit offtopic
So bash stands for Bourne-again shell. A (Unix) Shell is a command line user interface or maybe one could call it an interpreter (?)

So I'm filling out an application for a new job and you get asked for experience of different programming languages and then there is this box at the bottom other experiences - I started typing python 2.7, powershell, bas... Wait! bash isn't a programming language - it's a console that can execute shell scripts... so... eh.... oh my god I have no idea!

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

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.

Solution 3 - Bash

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

Solution 4 - Bash

My two cents

Comming on this SO question something late, reading fedorqui's answer, I think "programming language" is not exactly same thing than "command language", meaning a language intented to run commands.

About turing consideration, yes, you could... I personally wrote a lot of libraries around [tag:bash] (around monitoring, backups, sysadmin, networking, etc.), but clearly for writting a program, you have to use a real programming language.

However

[tag:bash] is a shell (like [tag:sh] and others [tag:shell])! Meaning an overall aggregator language, or a super language.

First goal is to be an interactive command processor, in order to use and maintain [tag:posix] systems.

One of his firsts applications was to create [tag:wrapper]s in order to prepare environment for running programs written in other languages.

So this command proccessor is ideal for systems, filesystems, networks and a lot of administation tasks, because it's interactive and using his history make creating script job just easy.

His real power

As this language is intended to deal with [tag:IO]s, [tag:fork]s, [tag:fifo]s and because [tag:POSIX] said everything is a file, a [tag:shell] script could normally deal with everything, directly or by using others tools/binaries/application. This language is intented to create condition, execution groups and interaction around everything.

This could open a lot of interactivity between systems, networks, iot, etc...

A script could for sample (see further my shell connector demo).

1. Open DB, SSH connection and log file simultaneously as file descriptors.
2. Create SQL (temporary or not) table
3. Doing loop, checking for event on DB, SSH connection or else...
4. Interact with DB and/or SSH...
5. Close all file descriptors (DB, SSH, log file, etc)

Mandelbrot sample:

Comments on Mecki's anwers show a good sample of how [tag:bash] could be used to deal with other binaries (bc for Mandelbrot)...

Where [tag:shell] is used to run bc and aggregate his answers.

  • If script do one fork for each calcul, this script will take many hours to draw a Mandelbrot on 80 columns terminal.
  • 1st improvement: running only one background fork to bc -l to submit all calculs, drop down execution time to 8 minutes.
  • 2nd improvement: passing iterate loop (upto 2000 tests) to bc, drop own execution time to 8 secondes.
  • 3nd improvement: creating more background bc for computing many dot simultaneously ([tag:parallel-processing]), in order to use multi-core, dividing execution time approximatively by available cores... (Thanks to Léa Gris for contributing, helping making this [tag:POSIX] compatible, multi-core idea and adding colors, making this near beautiful, I can't resist to post his result) Colored Mandelbrot in terminal

More sample

I wrote some scripts showing this powerfull parallelisation capabilities:

  • multiping.sh will run many ping simultaneously and draw a dynamic graphic using gnuplot, while staying interactive.
  • shell_connector.sh is a library if sourced but contain a full demo using sqlite, date and bc as background co-process if run.
  • getSo.sh is a script intented to connect on SO server, by using [tag:openssl] with authentication, cookies and Connection: keep-alive.

In order to do some monitoring, checks against differences or so one, we could create a script to open many simultaneous connections to many differents targets, using one of netcat, sql-client, ftp, open-ssl s_client, ssh or else...

... with the ability of running sha1sum, grep, xmlint or bc (if not already backgrounded) when required, while connections stays open...

Conclusion

[tag:shell] is a super-language, useful to aggregate a complex application using many programs in various languages.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionboopView Question on Stackoverflow
Solution 1 - BashfedorquiView Answer on Stackoverflow
Solution 2 - BashPaul EvansView Answer on Stackoverflow
Solution 3 - BashMeckiView Answer on Stackoverflow
Solution 4 - BashF. HauriView Answer on Stackoverflow