{{Infobox programming language | name = bs | paradigm = | year = {{start date and age|1980}} | designer = Richard C. Haight | developer = | typing = | implementations = | dialects = | influenced_by = BASIC, SNOBOL4, C | influenced = | operating_system = Unix | license = | website = | file_ext = }} {{lowercase}} '''<kbd>bs</kbd>''' is a programming language and a compiler/interpreter for modest-sized programs on UNIX systems.<ref name=SystemIIIR3/> The <kbd>bs</kbd> command can be invoked either for interactive programming or with a file containing a program, optionally taking arguments, via a Unix shell, e.g., using a Shebang (Unix) <kbd>#!/usr/bin/bs</kbd>.
An early man page states, "[<kbd>bs</kbd>] is a remote descendant of Basic [sic] and SNOBOL4, with a little C thrown in."<ref name=SystemIIIR3/>
== History ==
The '''<kbd>bs</kbd>''' command appears in UNIX System III Release 3.0 (1980), first released outside of Bell Labs in 1982.<ref name=SystemIIIR3/> It was written by Dick Haight (Richard C. Haight) circa 1978, who recounts it as follows:<ref name=DHaight>Personal communication from Dick Haight, 10 September 2019.</ref> {{Quote |text=I wrote <kbd>bs</kbd> at the time Unix (V 3?) and all of the commands were being converted from assembler to C. So [ Ken Thompson’s ] <kbd>bas</kbd> became my <kbd>bs</kbd> — sort of. }} The Release 3.0 manual mentions <kbd>bs</kbd> prominently on page 9 (emphasis added):<ref name=SystemIIIR3too/> {{Quote |text='''Writing a program.''' To enter the text of a source program into a UNIX file, use <kbd>ed(1)</kbd>. The four principal languages available under UNIX are C (see <kbd>cc(1)</kbd>), Fortran (see <kbd>f77(1)</kbd>), ''bs (a compiler/interpreter in the spirit of Basic, see <kbd>bs(1)</kbd>)'', and assembly language (see <kbd>as(1)</kbd>). }} While not released outside prior to System III, the '''<kbd>bs</kbd>''' command was present internally in UNIX/TS 1.0 (November 1978),<ref name=Mash>Personal conversation with John R. Mashey, 9 September 2019.</ref> PWB/UNIX 2.0 (June 1979),<ref name=Mash/> and CB UNIX editions 2.1 (November 1979)<ref name=CBUNIX21bs1>[https://www.tuhs.org/Archive/Distributions/USDL/CB_Unix/man/man1/bs.1.pdf "CB/UNIX bs(1)"], ''The Unix Heritage Society'', November 1979. Retrieved on 2 July 2025.</ref> and 2.3 (1981).<ref>{{cite book |editor=J. D. Doan |title=CB-UNIX Programmer's Manual, Edition 2.3 |date=May 1981 |page=iii |publisher=Bell Telephone Laboratories |location=Columbus, OH |url=http://www.tuhs.org/Archive/Distributions/USDL/CB_Unix/cbunix_intro.pdf}}</ref> The <kbd>bs</kbd> command does not appear in some earlier internal releases, e.g., the UNIX Support Group’s March 1977 release,<ref name=Mash/> nor the PWB/UNIX manual dated May, 1977,<ref>{{cite book |editor1=T. A. Dolotta |editor2=R. C. Haight|editor3=E. M. Piskorik |title=PWB UNIX Programmer's Manual, Edition 1|section = Section 1 |date=May 1977 |publisher=Bell Telephone Laboratories |location=Piscataway, New Jersey |url=https://archive.org/stream/bitsavers_attunixPWBSection1May77_11477153/PWB_UNIX_Edition_1_Section_1_May77_djvu.txt}}</ref> suggesting its creation circa 1978. It does not appear in any version of Research Unix nor the Berkeley Software Distribution.
Subsequently and into the 1990s, <kbd>bs</kbd> was included in a variety of System III-derived or System V-derived commercial operating systems including, but not limited to: PC/IX;<ref name=PCM/> UNIX System V Releases 2 & 3: SVR2,<ref name=3B1/> SVR3, SVR3.2 (1986);<ref name=SVR32/> HP-UX;<ref name=HPUX11i/> AIX;<ref name=AIX61/> and A/UX.<ref name=AUX/> (The User's Manual for the AT&T UNIX PC (3B1) specifically mentions that the <kbd>bs</kbd> command is not available, but that it is available on SVR3.2.<ref name=3B1/>)
Occasionally, <kbd>bs</kbd> was touted as one of the primary programming languages for development under UNIX.<ref name=SystemIIIR3too/><ref name=PCM/><ref>[http://support.apple.com/kb/TA39862?viewlocale=en_US "A/UX: Development Tools"], ''Apple, Inc'', 18 February 2012. Retrieved on 9 September 2019.</ref> However, <kbd>bs</kbd> is not included in the POSIX.1 commands and utilities (the standard List of Unix commands) nor in the Single UNIX Specification and is not provided with most contemporary operating systems. For example in Linux, similar syntax and functionality is provided by bc, Perl, and POSIX shell.
In the 21st century, <kbd>bs</kbd> is present in, at least, HP-UX Release 11i (2000),<ref name=HPUX11i/> as well as AIX versions 6.1 (2007)<ref name=AIX61/> and 7.2 (2018),<ref name=AIX72/> likely due to their UNIX System V heritage.
== Design and features ==
The <kbd>bs</kbd><ref name=SystemIIIR3/> man page, ostensibly the programming language's only specification, characterizes it as follows: {{Quote |text=''Bs'' is designed for programming tasks where program development time is as important as the resulting speed of execution. Formalities of data declaration and file/process manipulation are minimized. Line-at-a-time debugging, the <kbd>trace</kbd> and <kbd>dump</kbd> statements, and useful run-time error messages all simplify program testing. Furthermore, incomplete programs can be debugged; ''inner'' functions can be tested before ''outer'' functions have been written and vice versa. }}
A <kbd>bs</kbd> program is compiled and executed differently from programs written in the other principal Unix programming languages of the time: C, FORTRAN, and assembly language, whose respective commands compile program source code to executable assembler output (a.out). Instead, a <kbd>bs</kbd> program is, first, converted by the <kbd>bs</kbd> command to an internal reverse Polish (RPN) intermediate representation and then executed by the command's internal virtual stack machine.<ref>Personal conversation with Dick Haight, 12 September 2019.</ref> The <kbd>bs</kbd> language, thus, is a hybrid interpreter and compiler and a divergence in Unix programming from Ancient Unix. The <kbd>bs</kbd> language shares some features and syntax with BASIC, SNOBOL, and C, the two former presumably inspiring its name. Like BASIC, it can be used interactively, either executing statements immediately or collecting them into a program to be executed subsequently. Like in SNOBOL4, the assignment operator (<kbd>=</kbd>) is used for I/O and <kbd>bs</kbd> can execute code in strings, using its eval function. It also includes SNOBOL's interrogation operator (<kbd>?</kbd>) used to test whether an expression evaluation succeeds or not. The built-in <kbd>format</kbd> function, limited to one argument, supports a subset of C's <kbd>printf</kbd> format conversion specifiers, e.g., "%f".
The language has some conspicuous elements. For instance, its program functions are defined using the <kbd>fun '''...''' nuf</kbd> syntax and its functions can have local variables. Also, <kbd>bs</kbd> can operate in two modes, either interpreting (and executing) statements and programs or compiling them, and switching between the two using <kbd>compile</kbd> and <kbd>stop</kbd>. Otherwise, its functionality is unique only collectively (in one language), since individual features are redundant with those of coexisting tools, such as the Unix Shell, e.g., file I/O and loops, and AWK, e.g., associative arrays and Regular expression matching.
The <kbd>bs</kbd> language was meant for convenient development and debugging of small, modular programs. It has a collection of syntax and features from prior, popular languages but it is internally compiled, unlike a Shell script. As such, in purpose, design, and function, <kbd>bs</kbd> is a largely unknown, modest predecessor of hybrid interpreted/compiled languages such as Perl and Python.
== Syntax Examples ==
''The following examples are derived from an A/UX <kbd>bs(1)</kbd> man page.''<ref name=FILES/>
This example uses <kbd>bs</kbd> as a calculator: {{Quote |text=<syntaxhighlight lang="bash"> $ bs # Distance (inches) light travels in a nanosecond. 186000 * 5280 * 12 / 1e9 11.78496 ... # Compound interest # (6% for 5 years on $1,000). int = .06 / 4 bal = 1000 for i = 1 5*4 bal = bal + bal*int bal - 1000 346.855007 ... exit</syntaxhighlight> }}
This example is the outline of a typical <kbd>bs</kbd> program: {{Quote |text=<syntaxhighlight lang="bash"> # initialize things: var1 = 1 open("read", "infile", "r") ... # compute: while ?(str = read) ... next # clean up: close("read") ... # last statement executed (exit or stop): exit # last input line: run</syntaxhighlight> }}
This example demonstrates I/O: {{Quote |text=<syntaxhighlight lang="bash"> # Copy "oldfile" to "newfile". open("read", "oldfile", "r") open("write", "newfile", "w") ... while ?(write = read) ... # close "read" and "write": close("read") close("write") # Pipe between commands. open("ls", "!ls *", "r") open("pr", "!pr -2 -h ’List’", "w") while ?(pr = ls) ... ... # be sure to close (wait for) these: close("ls") close("pr")</syntaxhighlight> }}
== Sample Program ==
''The following is a sample <kbd>bs</kbd> program that emits the words to the song 99 Bottles of Beer using <kbd>/usr/bin/bs</kbd>.''<ref>[http://www.99-bottles-of-beer.net/language-bs-103.html "Language BS"], ''99 Bottles of Beer'', 8 August 1996. Retrieved on 9 September 2019.</ref> {{Quote |text=<syntaxhighlight lang="snobol"> fun sing(n, end) s s = ("s", "")[ match(n, "^1$") ] put = format(format(format("%s bottle%%s of beer%%%%s", n), s), end) nuf
for n = 99, n, put = "" sing(format("%-0.0f", n), " on the wall,") sing(format("%-0.0f", n), ",") put = "take one down, pass it around," --n sing((format("%-0.0f", n), "no")[ 0 == n ], " on the wall.") next </syntaxhighlight> }}
== References ==
<references> <ref name=SystemIIIR3>{{cite book |title = UNIX User's Manual |edition = Release 3.0 |year = 1980 |publisher = Bell Telephone Laboratories, Incorporated |page=95 |url=https://archive.org/details/bitsavers_attunixSysalRelease3Jun80_33886798/page/n95}} </ref> <ref name=SystemIIIR3too>{{cite book |title = UNIX User's Manual |edition = Release 3.0 |year = 1980 |publisher = Bell Telephone Laboratories, Incorporated |page=9 |url=http://bitsavers.informatik.uni-stuttgart.de/pdf/att/unix/System_III/UNIX_Users_Manual_Release_3_Jun80.pdf}} </ref> <ref name=SVR32>{{cite book |title = UNIX Programmer's Manual |section = Volume 1 Commands and Utilities |year = 1986 |publisher = AT&T |page = 41 |url=http://bitsavers.trailing-edge.com/pdf/att/unix/System_V_Release_2/UNIX_Programmers_Manual_Vol_1_Commands_and_Utilities_1986.pdf}} </ref> <ref name=3B1>{{cite book |title = AT&T UNIX PC UNIX System V User's Manual |section = Volume 1 |year = 1986 |publisher = AT&T |page=8 |url=http://bitsavers.trailing-edge.com/pdf/att/3b1/999-801-312IS_ATT_UNIX_PC_System_V_Users_Manual_Volume_1.pdf}} </ref> <ref name=AUX>{{cite book |title = A/UX Command Reference |edition = 2.0 |section = Section 1 (A-L) |year = 1990 |publisher = Apple Computer, Inc. |page=93 |url=http://bitsavers.org/pdf/apple/mac/a_ux/aux_2.0/030-0781_AUX_Command_Reference_Section_1_A-L_1990.pdf}} </ref> <ref name=HPUX11i>{{cite book |title = HP-UX Reference Release 11i User Commands |section = Section 1 (A-M) |edition = 1 |year = 2000 |publisher = Hewlett-Packard Company. |page=93 |url=http://h10032.www1.hp.com/ctg/Manual/c00386893.pdf}} </ref> <ref name=AIX61>{{cite book |title = AIX Version 6.1 Commands Reference |section = Section 1 (a-c) |edition = First |year = 2007 |publisher = International Business Machines Corporation. |page=251 |url=http://ps-2.kev009.com/basil.holloway/ALL%20PDF/SC23524300.pdf}} </ref> <ref name=AIX72>{{cite book |title = AIX Version 7.2 Commands Reference |section = Section 1 (a-c) |year = 2018 |publisher = IBM Corporation. |page=282 |url=https://www.scribd.com/document/400244565/aixcmds1-pdf-pdf}} </ref> <ref name=PCM>{{cite magazine |magazine=PC Magazine |date=June 12, 1984 | title=IBM Goes UNIX |page=218 }} </ref> <ref name=FILES>The <kbd>/FILES</kbd> file, A/UX 3.0.1 installation media, Apple Inc. (1993)</ref> </references>
Category:Programming languages Category:Unix_programming_tools