Shell one-liner: Difference between revisions

From Rosetta Code
Content added Content deleted
(C: "incorrect" notice removed -- see talk page)
(deleted incorrect examples as promised (belong to second category I listed on talk); changed <pre> to <lang> which seems to format spaces better)
Line 5: Line 5:


=={{header|Aikido}}==
=={{header|Aikido}}==
<lang aikido>
<lang aikido>echo 'println ("Hello")' | aikido</lang>
echo 'println ("Hello")' | aikido
</lang>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386 - Interpret straight off}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386 - Interpret straight off}}
<pre>$ a68g -e 'print(("Hello",new line))'</pre>
<lang>$ a68g -e 'print(("Hello",new line))'</lang>
Output:
Output:
<pre>Hello</pre>
<pre>Hello</pre>
{{works with|ELLA ALGOL 68|Any - tested with release 1.8.8d.fc9.i386 - translate to [[C]] and then compile and run}}
{{works with|ELLA ALGOL 68|Any - tested with release 1.8.8d.fc9.i386 - translate to [[C]] and then compile and run}}
For an [[ELLA ALGOL 68]] one-liner, merge these lines of shell code:
For an [[ELLA ALGOL 68]] one-liner, merge these lines of shell code:
<pre>code='print(("Hello", new line))'
<lang>code='print(("Hello", new line))'
a=/tmp/algol$$ s=/usr/share/algol68toc;
a=/tmp/algol$$ s=/usr/share/algol68toc;
echo -e "PROGRAM algol$$ CONTEXT VOID\nUSE standard\nBEGIN\n$code\nEND\nFINISH\n" > $a.a68 &&
echo -e "PROGRAM algol$$ CONTEXT VOID\nUSE standard\nBEGIN\n$code\nEND\nFINISH\n" > $a.a68 &&
a68toc -lib $s -dir $s -uname TMP -tmp $a.a68 && rm $a.a68 &&
a68toc -lib $s -dir $s -uname TMP -tmp $a.a68 && rm $a.a68 &&
gcc $s/Afirst.o $a.c -l{a68s,a68,m,c} -o $a && rm $a.c &&
gcc $s/Afirst.o $a.c -l{a68s,a68,m,c} -o $a && rm $a.c &&
$a; rm $a</pre>
$a; rm $a</lang>
Output:
Output:
<pre>Hello</pre>
<pre>Hello</pre>

=={{header|AutoHotkey}}==
{{incorrect|AutoHotkey|This is not a shell one-liner and is to be deleted if not corrected soon.}}
Note: whilst small, this is more than one line.
<lang autohotkey>RunWait, %comspec% /c systeminfo > tmp,,Hide
FileRead, var, tmp
FileDelete, tmp
MsgBox,% var</lang>


=={{header|AWK}}==
=={{header|AWK}}==
Line 68: Line 58:
Note that this is rather specific to [[Unix]]-like systems; most [[DOS]] and [[Windows]] interpreters are generally unable to handle programs in this manner, unless they were ported from a *nix system in the first place.
Note that this is rather specific to [[Unix]]-like systems; most [[DOS]] and [[Windows]] interpreters are generally unable to handle programs in this manner, unless they were ported from a *nix system in the first place.


=== {{Header|ZX Spectrum Basic}} ===
==={{Header|ZX Spectrum Basic}}===

On the ZX Spectrum, the ROM basic allows direct commands to be entered from the system prompt:
On the ZX Spectrum, the ROM basic allows direct commands to be entered from the system prompt:


<lang zxbasic>
<lang zxbasic>PRINT "Hello World!"</lang>
PRINT "Hello World!"
</lang>


=={{header|C}}==
=={{header|C}}==
Line 88: Line 75:


Requires PowerShell 2:
Requires PowerShell 2:
<pre>&gt; Add-Type -TypeDefinition "public class HelloWorld { public static void SayHi() { System.Console.WriteLine(""Hi!""); } }"
<lang>> Add-Type -TypeDefinition "public class HelloWorld { public static void SayHi() { System.Console.WriteLine(""Hi!""); } }"
&gt; [HelloWorld]::SayHi()
> [HelloWorld]::SayHi()
Hi!</pre>
Hi!</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 99: Line 86:
<lang bash>$ clj-env-dir -e "(defn add2 [x] (inc (inc x))) (add2 40)"
<lang bash>$ clj-env-dir -e "(defn add2 [x] (inc (inc x))) (add2 40)"
#'user/add2
#'user/add2
42
42</lang>
</lang>


=={{header|CMake}}==
=={{header|CMake}}==
Line 126: Line 112:
The <code>--src</code> option ends with the the filename extension the provided type of program would have:
The <code>--src</code> option ends with the the filename extension the provided type of program would have:


<pre>rune --src.e-awt 'def f := &lt;swing:makeJFrame>("Hello"); f.show(); f.addWindowListener(def _{to windowClosing(_) {interp.continueAtTop()} match _{}}); interp.blockAtTop()'</pre>
<lang>rune --src.e-awt 'def f := &lt;swing:makeJFrame>("Hello"); f.show(); f.addWindowListener(def _{to windowClosing(_) {interp.continueAtTop()} match _{}}); interp.blockAtTop()'</lang>


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
Line 138: Line 124:
=={{header|Erlang}}==
=={{header|Erlang}}==
Erlang always starts other applications that can run in parallel in the background, and as such will not die by itself. To kill erl, we sequentially run the 'halt' function from the 'erlang' module (the -S is there to guarantee 'halt' will be evaluated after the io function).
Erlang always starts other applications that can run in parallel in the background, and as such will not die by itself. To kill erl, we sequentially run the 'halt' function from the 'erlang' module (the -S is there to guarantee 'halt' will be evaluated after the io function).
$ erl -noshell -eval 'io:format("hello~n").' -s erlang halt
<lang>$ erl -noshell -eval 'io:format("hello~n").' -s erlang halt
hello
hello</lang>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
> echo printfn "Hello from F#" | fsi --quiet
<lang>> echo printfn "Hello from F#" | fsi --quiet
Hello from F#
Hello from F#</lang>


=={{header|Factor}}==
=={{header|Factor}}==
$ factor -run=none -e="USE: io \"hi\" print"
<lang>$ factor -run=none -e="USE: io \"hi\" print"</lang>


=={{header|Forth}}==
=={{header|Forth}}==
{{works with|GNU Forth}}
{{works with|GNU Forth}}
$ gforth -e ".( Hello) cr bye"
<lang>$ gforth -e ".( Hello) cr bye"
Hello
Hello</lang>


=={{header|Gema}}==
=={{header|Gema}}==
$ gema -p '\B=Hello\n@end'
<lang>$ gema -p '\B=Hello\n@end'
Hello
Hello</lang>

=={{header|Go}}==
=={{header|Go}}==
Go is first of all a compiled language and currently comes with no support for running as a script language. The compiler and linker can of course be run from a command line shell, as in,
Go is first of all a compiled language and currently comes with no support for running as a script language. The compiler and linker can of course be run from a command line shell, as in,
<lang bash>echo 'package main;func main(){println("hllowrld")}'>8.go;8g 8.go;8l 8.8;8.out</lang>
<pre>
echo 'package main;func main(){println("hllowrld")}'>8.go;8g 8.go;8l 8.8;8.out
</pre>
This will overwrite existing files in the current directory 8.go, 8.8, and 8.out, assuming of course, that the the current directory is even writable, and will leave these files behind after executing.
This will overwrite existing files in the current directory 8.go, 8.8, and 8.out, assuming of course, that the the current directory is even writable, and will leave these files behind after executing.


Running Go as a script language is a popular request however, and one of the better solutions currently is [https://wiki.ubuntu.com/gorun gorun]. Gorun has solutions for the temporary file problem, writing to best-guess temporary directories by default and having an option to specify the location when this is needed or desired. Gorun still expects to read the source code from a file however, so you are on your own to deal with this before passing the file to gorun. Example,
Running Go as a script language is a popular request however, and one of the better solutions currently is [https://wiki.ubuntu.com/gorun gorun]. Gorun has solutions for the temporary file problem, writing to best-guess temporary directories by default and having an option to specify the location when this is needed or desired. Gorun still expects to read the source code from a file however, so you are on your own to deal with this before passing the file to gorun. Example,
<lang bash>echo 'package main;func main(){println("hllowrld")}'>/tmp/8.go;gorun /tmp/8.go</lang>
<pre>
echo 'package main;func main(){println("hllowrld")}'>/tmp/8.go;gorun /tmp/8.go
</pre>
Output from either example:
Output from either example:
<pre>
<pre>
Line 174: Line 157:
=={{header|Groovy}}==
=={{header|Groovy}}==
{{works with|UNIX Shell}}
{{works with|UNIX Shell}}
<pre>$ groovysh -q "println 'Hello'"
<lang>$ groovysh -q "println 'Hello'"
Hello</pre>
Hello</lang>


{{works with|Windows Command Interpreter}}
{{works with|Windows Command Interpreter}}
<pre>C:\Users\user> groovysh -q "println 'Hello'"
<lang>C:\Users\user> groovysh -q "println 'Hello'"
Hello</pre>
Hello</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==
<pre>$ ghc -e 'putStrLn "Hello"'
<lang>$ ghc -e 'putStrLn "Hello"'
Hello</pre>
Hello</lang>


=={{header|J}}==
=={{header|J}}==
<pre>$ jconsole -js "exit echo 'Hello'"
<lang>$ jconsole -js "exit echo 'Hello'"
Hello</pre>
Hello</lang>


That said, note that J interpreters can themselves be thought of as [[wp:Command_shell|command shells]].
That said, note that J interpreters can themselves be thought of as [[wp:Command_shell|command shells]].
Line 204: Line 187:


Works with cmd.exe on Windows (tested on Microsoft Windows XP [Version 5.1.2600])
Works with cmd.exe on Windows (tested on Microsoft Windows XP [Version 5.1.2600])
<lang>C:\>echo public class X{public static void main(String[] args){System.out.println("Hello Java!");}}>X.java&&javac X.java&&java X
<PRE>
Hello Java!</lang>
C:\>echo public class X{public static void main(String[] args){System.out.println("Hello Java!");}}>X.java&&javac X.java&&java X
Hello Java!

</PRE>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
{{works with|SpiderMonkey}}
{{works with|SpiderMonkey}}
<pre>$ js -e 'print("hello")'
<lang>$ js -e 'print("hello")'
hello</pre>
hello</lang>
=={{header|Lua}}==
=={{header|Lua}}==
<pre>lua -e 'print "Hello World!"'</pre>
<lang>lua -e 'print "Hello World!"'</lang>

=={{header|MATLAB}}==
{{incorrect|Matlab|This is not a shell one-liner and is to be deleted if not corrected soon.}}
Note: whilst small, this is more than one line.

The command line in MATLAB acts just like the "main()" function in Java. It's a state machine. Variables declared in the scope of the command line persist in that scope until cleared from memory. So any thing you could do in a script, you can execute by directly input the desired commands into the command line. This first example declares and initializes an array named "a", which is stored in the scope of the MATLAB environment. Then the next command computes the mean of the elements in "a".

Example:
<lang MATLAB>>> a = [1 2 3];
>> mean(a)

ans =

2</lang>

It is possible to declare a function in the command line thusly:
<lang MATLAB>>> helloWorld = @()disp('Hello World');
>> helloWorld()
Hello World</lang>

=={{header|MUMPS}}==
{{incorrect|MUMPS|This is not a shell one-liner and is to be deleted if not corrected soon.}}
Note: while small, this assumes the REPL is already invoked.

<pre>
USER>WRITE !,"Hello nice people"

Hello, nice people
USER>DO HAPPY^ROSETTA(3)
The first 3 happy numbers are:
1
7
10
</pre>


=={{header|Objeck}}==
=={{header|Objeck}}==
{{works with|UNIX Shell}}
{{works with|UNIX Shell}}
<pre>./obc -run '"Hello"->PrintLine();' -dest hello.obe ; ./obr hello.obe</pre>
<lang>./obc -run '"Hello"->PrintLine();' -dest hello.obe ; ./obr hello.obe</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
<pre>$ ocaml <(echo 'print_endline "Hello"')
<lang>$ ocaml <(echo 'print_endline "Hello"')
Hello</pre>
Hello</lang>


=={{header|Oz}}==
=={{header|Oz}}==
This is difficult to do in Oz because the compiler/interpreter always wants the source code in a file and does not read from stdin. We can do somethings like this on Unix-like systems:
This is difficult to do in Oz because the compiler/interpreter always wants the source code in a file and does not read from stdin. We can do somethings like this on Unix-like systems:
<lang bash>echo >tmp.oz "{System.show hello}"; ozc -l System -e tmp.oz
<pre>
hello</lang>
echo >tmp.oz "{System.show hello}"; ozc -l System -e tmp.oz
hello
</pre>


With <code>-l System</code> we make the System module available so that we can print something.
With <code>-l System</code> we make the System module available so that we can print something.
=={{header|PARI/GP}}==
{{incorrect|PARI/GP|This is not a shell one-liner and is to be deleted if not corrected soon.}}
<lang parigp>system("ls")</lang>


=={{header|Perl}}==
=={{header|Perl}}==
<pre>$ perl -e 'print "Hello\n"'
<lang>$ perl -e 'print "Hello\n"'
Hello</pre>
Hello</lang>


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|#22 "Thousand Oaks"}}
{{works with|Rakudo|#22 "Thousand Oaks"}}


<pre>$ perl6 -e 'say "Hello, world!"'
<lang>$ perl6 -e 'say "Hello, world!"'
Hello, world!</pre>
Hello, world!</lang>


=={{header|PHP}}==
=={{header|PHP}}==
assuming you have the PHP CLI (command-line interface) installed, not just the web server plugin
assuming you have the PHP CLI (command-line interface) installed, not just the web server plugin
<pre>$ php -r 'echo "Hello\n";'
<lang>$ php -r 'echo "Hello\n";'
Hello</pre>
Hello</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Line 297: Line 236:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<pre>> powershell -Command "Write-Host 'Hello'"
<lang>> powershell -Command "Write-Host 'Hello'"
Hello</pre>
Hello</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
Runs on Linux with(thanks to) bash. Path variables must be set as decribed in INSTALL.
Runs on Linux with(thanks to) bash. Path variables must be set as decribed in INSTALL.
<pre>$ echo 'messagerequester("Greetings","hello")' > "dib.pb" && ./pbcompiler dib.pb -e "dib" && ./dib</pre>
<lang>$ echo 'messagerequester("Greetings","hello")' > "dib.pb" && ./pbcompiler dib.pb -e "dib" && ./dib</lang>


=={{header|Python}}==
=={{header|Python}}==
===Prints "Hello"===
===Prints "Hello"===
<pre>$ python -c 'print "Hello"'
<lang>$ python -c 'print "Hello"'
Hello</pre>
Hello</lang>


===Web server with CGI===
===Web server with CGI===
The python CGIHTTPServer module is also an [[Executable library|executable library]] that performs as a web server with CGI. to start enter:
The python CGIHTTPServer module is also an [[Executable library|executable library]] that performs as a web server with CGI. to start enter:
<pre>python -m CGIHTTPServer</pre>
<lang bash>python -m CGIHTTPServer</lang>
It returns with:
It returns with:
<pre>Serving HTTP on 0.0.0.0 port 8000 ...</pre>
<lang>Serving HTTP on 0.0.0.0 port 8000 ...</lang>


=={{header|R}}==
=={{header|R}}==
<lang>$ echo 'cat("Hello\n")' | R --slave

Hello</lang>
<pre>$ echo 'cat("Hello\n")' | R --slave
Hello
</pre>


Alternatively, using the Rscript front-end,
Alternatively, using the Rscript front-end,


<pre>$ Rscript -e 'cat("Hello\n")'
<lang>$ Rscript -e 'cat("Hello\n")'
Hello
Hello</lang>
</pre>


=={{header|REBOL}}==
=={{header|REBOL}}==
<pre>rebview -vswq --do "print {Hello!} quit" </pre>
<lang bash>rebview -vswq --do "print {Hello!} quit" </lang>


Output:
Output:


<pre>Hello!</pre>
<lang>Hello!</lang>


=={{header|Retro}}==
=={{header|Retro}}==
<lang Retro>echo '"hello\n" puts bye' | ./retro</lang>
<lang Retro>echo '"hello\n" puts bye' | ./retro</lang>

=={{header|REXX}}==
{{incorrect|REXX|This is not a shell one-liner and is to be deleted if not corrected soon.}}
To execute a REXX program depends largely on the operating system.

Where REXX was originally developed ([[wp:VM (operating system)|VM/CMS]]), there was native support for EXECs (which REXX is a type of); the REXX program had a filetype of 'EXEC' and all one needed to do to invoke it was to just enter its filename.

A REXX program may be named (say):
<lang rexx>SOMESUCH EXEC A1</lang>

''SOMESUCH'' is the filename
''EXEC'' is the filetype (the file extension)
''A1'' is the filemode (A would be like a harddrive letter)

To execute it, enter:
<lang rexx>SOMESUCH ...optionalParameters...</lang>
or
<lang rexx>EXEC SOMESUCH ...optionalParameters...</lang>

In [[wp:MVS|MVS]]/[[wp:Time Sharing Option|TSO]], 'SOMESUCH' would be placed in an appropiate PDS ([[wp:Data set (IBM mainframe)#Partitioned datasets|partitioned dataset]]) library, and the following command could be issued:
<lang rexx>SOMESUCH</lang>

In CMS (VM), TSO (MVS), and [[OS/2]] (among others in the IBM family), the REXX interpretor is built in the operating system.

In [[DOS]] (under [[Windows]] or not), most REXX interpretors assume a REXX program has a particular file extension (or one of serveral extensions, 'REX', 'REXX', 'CMD' being the most common), and to invoke a REXX interpretor to execute (interpret) a REXX program, the name of the REXX interpreter is issued followed by the REXX program name (and followed by any optional parameters).

Alternatively, in the [[Windows NT]] family of operating systems (which includes 2000, XP, Vista, 7, and up), a program can be associated with a file's extension, so when the following is issued:
<lang rexx>SOMESUCH</lang>
Windows (or DOS) would look in the usual places (usually starting in the current directory, and including, but not limited to the PATH, it would find SOMESUCH.REX (for instance), and note that files with such file extension should be executed with the appropriate program, for example REXX.EXE, REGINA.EXE, or R4.EXE (just to name a few examples, but only one could be specified), which itself, could be in the PATH or be specified as a specific file such as <code>C:\LANGUAGES\REXX\REXX.EXE</code> (just an example).


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 383: Line 290:


=={{header|Scala}}==
=={{header|Scala}}==
<pre>C:\>scala -e "println(\"Hello\")"
<lang>C:\>scala -e "println(\"Hello\")"
Hello</pre>
Hello</lang>
The escaping of quotes is required by Windows. On Unix, one could just use single quotes around the code. In either case, any required libraries should have their JAR files pointed at by the environment variable CLASSPATH.
The escaping of quotes is required by Windows. On Unix, one could just use single quotes around the code. In either case, any required libraries should have their JAR files pointed at by the environment variable CLASSPATH.


Line 407: Line 314:
=={{header|Tcl}}==
=={{header|Tcl}}==
This is an area where Tcl is lacking, though when shell one-liners are required a construct like this is typically used:
This is an area where Tcl is lacking, though when shell one-liners are required a construct like this is typically used:
<pre>$ echo 'puts Hello' | tclsh
<lang>$ echo 'puts Hello' | tclsh
Hello</pre>
Hello</lang>
(This apparent weakness is purposeful; it leaves a larger fraction of the space of possible arguments open to use by the script being executed.)
(This apparent weakness is purposeful; it leaves a larger fraction of the space of possible arguments open to use by the script being executed.)


Line 416: Line 323:
Invoking from the shell, a multi-line TXR query is passed using -c as a single argument. It invokes the shell command using bang notation in <code>@(next)</code>, collect its output and dumps the bindings:
Invoking from the shell, a multi-line TXR query is passed using -c as a single argument. It invokes the shell command using bang notation in <code>@(next)</code>, collect its output and dumps the bindings:


<pre>$ txr -c '@(next `!for x in 0 1 2 3 4; do echo $x; done`)
<lang>$ txr -c '@(next `!for x in 0 1 2 3 4; do echo $x; done`)
@(collect)
@(collect)
@X
@X
Line 425: Line 332:
X[2]="2"
X[2]="2"
X[3]="3"
X[3]="3"
X[4]="4"</pre>
X[4]="4"</lang>


{{works with|bash}}
{{works with|bash}}
Line 431: Line 338:
If we eval the above command back into the shell, we create an array (if the shell supports array syntax).
If we eval the above command back into the shell, we create an array (if the shell supports array syntax).


<pre>$ eval $(txr -c '@(next `!for x in 0 1 2 3 4; do echo $x; done`)
<lang>$ eval $(txr -c '@(next `!for x in 0 1 2 3 4; do echo $x; done`)
@(collect)
@(collect)
@X
@X
Line 437: Line 344:
')
')
$ echo ${X[*]}
$ echo ${X[*]}
0 1 2 3 4</pre>
0 1 2 3 4</lang>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Line 473: Line 380:
type, it can be formatted for display by --cast <type expression>,
type, it can be formatted for display by --cast <type expression>,


<pre>$ fun --main=-[hello]- --show
<lang>$ fun --main=-[hello]- --show
hello
hello
$ fun --main="power/2 32" --cast %n
$ fun --main="power/2 32" --cast %n
4294967296
4294967296
$ fun --m="..mp2str mpfr..pi 120" --c %s
$ fun --m="..mp2str mpfr..pi 120" --c %s
'3.1415926535897932384626433832795028847E+00'</pre>
'3.1415926535897932384626433832795028847E+00'</lang>


=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==
Line 484: Line 391:
The following DOS command starts Vedit and displays a message.
The following DOS command starts Vedit and displays a message.
When the user presses any key, Vedit exits.
When the user presses any key, Vedit exits.
<pre>vpw -c'Get_Key("Hello!") exit'</pre>
<lang>vpw -c'Get_Key("Hello!") exit'</lang>


{{omit from|Ada}}
{{omit from|Ada}}

Revision as of 09:54, 2 October 2011

Task
Shell one-liner
You are encouraged to solve this task according to the task description, using any language you may know.

Show how to specify and execute a short program in the language from a command shell, where the input to the command shell is only one line in length.

Avoid depending on the particular shell or operating system used as much as is reasonable; if the language has notable implementations which have different command argument syntax, or the systems those implementations run on have different styles of shells, it would be good to show multiple examples.

Aikido

<lang aikido>echo 'println ("Hello")' | aikido</lang>

ALGOL 68

Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386 - Interpret straight off

<lang>$ a68g -e 'print(("Hello",new line))'</lang> Output:

Hello
Works with: ELLA ALGOL 68 version Any - tested with release 1.8.8d.fc9.i386 - translate to C and then compile and run

For an ELLA ALGOL 68 one-liner, merge these lines of shell code: <lang>code='print(("Hello", new line))' a=/tmp/algol$$ s=/usr/share/algol68toc; echo -e "PROGRAM algol$$ CONTEXT VOID\nUSE standard\nBEGIN\n$code\nEND\nFINISH\n" > $a.a68 && a68toc -lib $s -dir $s -uname TMP -tmp $a.a68 && rm $a.a68 && gcc $s/Afirst.o $a.c -l{a68s,a68,m,c} -o $a && rm $a.c && $a; rm $a</lang> Output:

Hello

AWK

Maybe the most common way one can use awk is from the command line for one-liners, feeding the interpreter with an input.

$ awk 'BEGIN { print "Hello"; }'

A more "complex" and "real" example:

$ awk '/IN/ { print $2, $4; }' <input.txt

Select field 2 and 4 of lines matching the regular expression /IN/ (i.e. where IN appears)

BASIC

The name of the BASIC executable will vary (common ones are basic, bas, and bwbasic), but in general, a short program can be piped to the interpreter like any other language: <lang bash>echo 'print "foo"'|basic</lang>

Note that under Windows (and presumably DOS) the two apostrophes (a.k.a. single quotes) should be omitted, since Windows doesn't remove them from the piped text (and the apostrophe is the comment character in many modern BASICs): <lang dos>echo print "foo"|basic</lang>

Also, some popular interpreters (including Michael Haardt's bas and Chipmunk Basic) will include an extra prompt before exiting unless you include exit or system (depending on the specific interpreter's syntax). This sample output shows both with and without system in bas:

erik@satan:~$ echo 'print "foo"'|bas
bas 2.2
Copyright 1999-2009 Michael Haardt.
This is free software with ABSOLUTELY NO WARRANTY.
> foo
> erik@satan:~$ echo 'print "foo":system'|bas
bas 2.2
Copyright 1999-2009 Michael Haardt.
This is free software with ABSOLUTELY NO WARRANTY.
> foo
erik@satan:~$

Note that this is rather specific to Unix-like systems; most DOS and Windows interpreters are generally unable to handle programs in this manner, unless they were ported from a *nix system in the first place.

ZX Spectrum Basic

On the ZX Spectrum, the ROM basic allows direct commands to be entered from the system prompt:

<lang zxbasic>PRINT "Hello World!"</lang>

C

Works with: gcc

The following code leaves the file a.out in the current directory (it does not delete it to avoid to call another shell/system dependent command/program). The current directory is not specified by ./ in every system...

$ echo 'main() {printf("Hello\n");}' | gcc -w -x c -; ./a.out

C#

Note: whilst small, this is more than one line.

Requires PowerShell 2: <lang>> Add-Type -TypeDefinition "public class HelloWorld { public static void SayHi() { System.Console.WriteLine(""Hi!""); } }" > [HelloWorld]::SayHi() Hi!</lang>

Clojure

Note: whilst small, this is more than one line.

clj-env-dir comes with clojure-contrib.

<lang bash>$ clj-env-dir -e "(defn add2 [x] (inc (inc x))) (add2 40)"

  1. 'user/add2

42</lang>

CMake

This only works with Unix systems that have the device node /dev/stdin.

<lang bash>echo 'message(STATUS "Goodbye, World!")' | cmake -P /dev/stdin</lang>

Common Lisp

Varies by implementation

Works with: SBCL

<lang bash>sbcl --noinform --eval '(progn (princ "Hello") (terpri) (quit))'</lang>

Works with: CLISP

<lang bash>clisp.exe -q -x "(progn (format t \"Hello from CLISP\") (quit))"</lang>

D

Works with: D version 2

requires rdmd <lang d>rdmd --eval="writeln(q{Hello World!})"</lang>

Hello World!

E

<lang bash>rune --src.e 'println("Hello")'</lang>

The --src option ends with the the filename extension the provided type of program would have:

<lang>rune --src.e-awt 'def f := <swing:makeJFrame>("Hello"); f.show(); f.addWindowListener(def _{to windowClosing(_) {interp.continueAtTop()} match _{}}); interp.blockAtTop()'</lang>

Emacs Lisp

<lang bash>emacs -batch -eval '(princ "Hello World!\n")' </lang>

Or another example that does something useful: indent a C source file:

<lang bash>emacs -batch sample.c --eval '(indent-region (point-min) (point-max) nil)' -f save-buffer</lang>

Erlang

Erlang always starts other applications that can run in parallel in the background, and as such will not die by itself. To kill erl, we sequentially run the 'halt' function from the 'erlang' module (the -S is there to guarantee 'halt' will be evaluated after the io function). <lang>$ erl -noshell -eval 'io:format("hello~n").' -s erlang halt hello</lang>

F#

<lang>> echo printfn "Hello from F#" | fsi --quiet Hello from F#</lang>

Factor

<lang>$ factor -run=none -e="USE: io \"hi\" print"</lang>

Forth

Works with: GNU Forth

<lang>$ gforth -e ".( Hello) cr bye" Hello</lang>

Gema

<lang>$ gema -p '\B=Hello\n@end' Hello</lang>

Go

Go is first of all a compiled language and currently comes with no support for running as a script language. The compiler and linker can of course be run from a command line shell, as in, <lang bash>echo 'package main;func main(){println("hllowrld")}'>8.go;8g 8.go;8l 8.8;8.out</lang> This will overwrite existing files in the current directory 8.go, 8.8, and 8.out, assuming of course, that the the current directory is even writable, and will leave these files behind after executing.

Running Go as a script language is a popular request however, and one of the better solutions currently is gorun. Gorun has solutions for the temporary file problem, writing to best-guess temporary directories by default and having an option to specify the location when this is needed or desired. Gorun still expects to read the source code from a file however, so you are on your own to deal with this before passing the file to gorun. Example, <lang bash>echo 'package main;func main(){println("hllowrld")}'>/tmp/8.go;gorun /tmp/8.go</lang> Output from either example:

hllowrld

Groovy

Works with: UNIX Shell

<lang>$ groovysh -q "println 'Hello'" Hello</lang>

<lang>C:\Users\user> groovysh -q "println 'Hello'" Hello</lang>

Haskell

<lang>$ ghc -e 'putStrLn "Hello"' Hello</lang>

J

<lang>$ jconsole -js "exit echo 'Hello'" Hello</lang>

That said, note that J interpreters can themselves be thought of as command shells.

Java

These three lines work with Bourne Shell or C Shell on Unix/Linux/MacOSX/Windows+cygwin

<lang bash>$ echo 'public class X{public static void main(String[]args){' \ > 'System.out.println("Hello Java!");}}' >X.java $ javac X.java && java X </lang>

A user can also enter this as one (very long) line:

<lang bash>$ echo 'public class X{public static void main(String[]args){System.out.println("Hello Java!");}}'>X.java;javac X.java&&java X</lang>

Works with cmd.exe on Windows (tested on Microsoft Windows XP [Version 5.1.2600]) <lang>C:\>echo public class X{public static void main(String[] args){System.out.println("Hello Java!");}}>X.java&&javac X.java&&java X Hello Java!</lang>

JavaScript

Works with: SpiderMonkey

<lang>$ js -e 'print("hello")' hello</lang>

Lua

<lang>lua -e 'print "Hello World!"'</lang>

Objeck

Works with: UNIX Shell

<lang>./obc -run '"Hello"->PrintLine();' -dest hello.obe ; ./obr hello.obe</lang>

OCaml

<lang>$ ocaml <(echo 'print_endline "Hello"') Hello</lang>

Oz

This is difficult to do in Oz because the compiler/interpreter always wants the source code in a file and does not read from stdin. We can do somethings like this on Unix-like systems: <lang bash>echo >tmp.oz "{System.show hello}"; ozc -l System -e tmp.oz hello</lang>

With -l System we make the System module available so that we can print something.

Perl

<lang>$ perl -e 'print "Hello\n"' Hello</lang>

Perl 6

Works with: Rakudo version #22 "Thousand Oaks"

<lang>$ perl6 -e 'say "Hello, world!"' Hello, world!</lang>

PHP

assuming you have the PHP CLI (command-line interface) installed, not just the web server plugin <lang>$ php -r 'echo "Hello\n";' Hello</lang>

PicoLisp

<lang PicoLisp>$ picolisp -'prinl "Hello world!"' -bye Hello world!</lang>

Pike

<lang pike>$ pike -e 'write("Hello\n");' Hello</lang>

PowerShell

<lang>> powershell -Command "Write-Host 'Hello'" Hello</lang>

PureBasic

Runs on Linux with(thanks to) bash. Path variables must be set as decribed in INSTALL. <lang>$ echo 'messagerequester("Greetings","hello")' > "dib.pb" && ./pbcompiler dib.pb -e "dib" && ./dib</lang>

Python

Prints "Hello"

<lang>$ python -c 'print "Hello"' Hello</lang>

Web server with CGI

The python CGIHTTPServer module is also an executable library that performs as a web server with CGI. to start enter: <lang bash>python -m CGIHTTPServer</lang> It returns with: <lang>Serving HTTP on 0.0.0.0 port 8000 ...</lang>

R

<lang>$ echo 'cat("Hello\n")' | R --slave Hello</lang>

Alternatively, using the Rscript front-end,

<lang>$ Rscript -e 'cat("Hello\n")' Hello</lang>

REBOL

<lang bash>rebview -vswq --do "print {Hello!} quit" </lang>

Output:

<lang>Hello!</lang>

Retro

<lang Retro>echo '"hello\n" puts bye' | ./retro</lang>

Ruby

From Unix:

<lang bash>$ ruby -e 'puts "Hello"' Hello</lang>

From Unix, to other implementations of Ruby:

Works with: JRuby

<lang bash>$ jruby -rjava -e'java.lang.System.out.println("Hello from JRuby")' Hello from JRuby</lang>

Works with: Rubinius

<lang bash>$ rbx -ractor -e'Actor.spawn {puts "Hello from Rubinius"}' Hello from Rubinius</lang>

Scala

<lang>C:\>scala -e "println(\"Hello\")" Hello</lang> The escaping of quotes is required by Windows. On Unix, one could just use single quotes around the code. In either case, any required libraries should have their JAR files pointed at by the environment variable CLASSPATH.

Scheme

Works with: Guile

<lang scheme>guile -c '(display "Hello, world!\n")'</lang>

Shiny

<lang shiny>shiny -e "say 'hi'" </lang>

Slate

<lang slate>./slate --eval "[inform: 'hello'] ensure: [exit: 0].".</lang>

SNOBOL4

Portable version <lang snobol>echo 'a output = "Hello, World!";end' | snobol4 -b</lang>

Bash version <lang snobol>snobol4 -b <<<'a output = "Hello, World!";end'</lang>


Tcl

This is an area where Tcl is lacking, though when shell one-liners are required a construct like this is typically used: <lang>$ echo 'puts Hello' | tclsh Hello</lang> (This apparent weakness is purposeful; it leaves a larger fraction of the space of possible arguments open to use by the script being executed.)

TXR

Note: whilst small, this is more than one line.

Invoking from the shell, a multi-line TXR query is passed using -c as a single argument. It invokes the shell command using bang notation in @(next), collect its output and dumps the bindings:

<lang>$ txr -c '@(next `!for x in 0 1 2 3 4; do echo $x; done`) @(collect) @X @(end) ' X[0]="0" X[1]="1" X[2]="2" X[3]="3" X[4]="4"</lang>

Works with: bash

If we eval the above command back into the shell, we create an array (if the shell supports array syntax).

<lang>$ eval $(txr -c '@(next `!for x in 0 1 2 3 4; do echo $x; done`) @(collect) @X @(end) ') $ echo ${X[*]} 0 1 2 3 4</lang>

UNIX Shell

Explicit call of the shell, passing the shell command via the -c option:

<lang bash>$ sh -c ls</lang>

<lang bash>$ sh -c "echo hello"</lang>

To invoke a specific shell like Bash, Korn Shell or Z Shell:

<lang bash>$ bash -c 'paste <(echo 1) <(echo 2)' $ ksh -c 'let i=3+4; print $i' $ zsh -c 'if 5 -lt 6 { echo ok };'</lang>

Shell scripts almost never use sh -c, because there are various implicit ways whereby the shell command language evaluates a command in a subshell:

<lang bash>$ VAR=`echo hello` # obsolescent backtick notation $ VAR=$(echo hello) # modern POSIX notation $ (echo hello) # execute in another shell process, not in this one</lang>

There are more details about `echo hello` and $(echo hello) at Execute a system command#UNIX Shell.

C Shell

Run a C shell command from any shell:

<lang bash>$ csh -fc 'if (5 < 6) echo ok'</lang>

Ursala

The command to execute the Ursala compiler is fun. An expression supplied as a parameter to the --main option is compiled and evaluated. If the expression evaluates to a list of character strings, it can be displayed on standard output with --show. If it's some other type, it can be formatted for display by --cast <type expression>,

<lang>$ fun --main=-[hello]- --show hello $ fun --main="power/2 32" --cast %n 4294967296 $ fun --m="..mp2str mpfr..pi 120" --c %s '3.1415926535897932384626433832795028847E+00'</lang>

Vedit macro language

The following DOS command starts Vedit and displays a message. When the user presses any key, Vedit exits. <lang>vpw -c'Get_Key("Hello!") exit'</lang>