Sophie

Sophie

distrib > Fedora > 20 > x86_64 > by-pkgid > d9f573299e87e886807be879704f0b6e > files > 141

julia-doc-0.3.4-1.fc20.noarch.rpm




<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Running External Programs &mdash; Julia Language 0.3.4 documentation</title>
  

  
  

  
  <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>

  
  
    

  

  
  
    <link rel="stylesheet" href="../_static/julia.css" type="text/css" />
  

  
    <link rel="stylesheet" href="../_static/julia.css" type="text/css" />
  
    <link rel="top" title="Julia Language 0.3.4 documentation" href="../index.html"/>
        <link rel="next" title="Calling C and Fortran Code" href="calling-c-and-fortran-code.html"/>
        <link rel="prev" title="Parallel Computing" href="parallel-computing.html"/> 

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-nav-search">
        <a href="http://julialang.org/"><img src="../_static/julia-logo.svg" class="logo"></a>
        <!--
        <a href="../index.html" class="fa fa-home"> Julia Language</a>
        -->
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
        
        
            <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="introduction.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="getting-started.html">Getting Started</a><ul>
<li class="toctree-l2"><a class="reference internal" href="getting-started.html#resources">Resources</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="variables.html">Variables</a><ul>
<li class="toctree-l2"><a class="reference internal" href="variables.html#allowed-variable-names">Allowed Variable Names</a></li>
<li class="toctree-l2"><a class="reference internal" href="variables.html#stylistic-conventions">Stylistic Conventions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="integers-and-floating-point-numbers.html">Integers and Floating-Point Numbers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="integers-and-floating-point-numbers.html#integers">Integers</a></li>
<li class="toctree-l2"><a class="reference internal" href="integers-and-floating-point-numbers.html#floating-point-numbers">Floating-Point Numbers</a></li>
<li class="toctree-l2"><a class="reference internal" href="integers-and-floating-point-numbers.html#arbitrary-precision-arithmetic">Arbitrary Precision Arithmetic</a></li>
<li class="toctree-l2"><a class="reference internal" href="integers-and-floating-point-numbers.html#numeric-literal-coefficients">Numeric Literal Coefficients</a></li>
<li class="toctree-l2"><a class="reference internal" href="integers-and-floating-point-numbers.html#literal-zero-and-one">Literal zero and one</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="mathematical-operations.html">Mathematical Operations and Elementary Functions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="mathematical-operations.html#arithmetic-operators">Arithmetic Operators</a></li>
<li class="toctree-l2"><a class="reference internal" href="mathematical-operations.html#bitwise-operators">Bitwise Operators</a></li>
<li class="toctree-l2"><a class="reference internal" href="mathematical-operations.html#updating-operators">Updating operators</a></li>
<li class="toctree-l2"><a class="reference internal" href="mathematical-operations.html#numeric-comparisons">Numeric Comparisons</a></li>
<li class="toctree-l2"><a class="reference internal" href="mathematical-operations.html#elementary-functions">Elementary Functions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="complex-and-rational-numbers.html">Complex and Rational Numbers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="complex-and-rational-numbers.html#complex-numbers">Complex Numbers</a></li>
<li class="toctree-l2"><a class="reference internal" href="complex-and-rational-numbers.html#rational-numbers">Rational Numbers</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="strings.html">Strings</a><ul>
<li class="toctree-l2"><a class="reference internal" href="strings.html#characters">Characters</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#string-basics">String Basics</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#unicode-and-utf-8">Unicode and UTF-8</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#interpolation">Interpolation</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#common-operations">Common Operations</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#non-standard-string-literals">Non-Standard String Literals</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#regular-expressions">Regular Expressions</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#id3">Byte Array Literals</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#version-number-literals">Version Number Literals</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="functions.html">Functions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="functions.html#argument-passing-behavior">Argument Passing Behavior</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#the-return-keyword">The <tt class="docutils literal"><span class="pre">return</span></tt> Keyword</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#id1">Operators Are Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#operators-with-special-names">Operators With Special Names</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#anonymous-functions">Anonymous Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#multiple-return-values">Multiple Return Values</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#varargs-functions">Varargs Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#optional-arguments">Optional Arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#keyword-arguments">Keyword Arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#evaluation-scope-of-default-values">Evaluation Scope of Default Values</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#block-syntax-for-function-arguments">Block Syntax for Function Arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#further-reading">Further Reading</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="control-flow.html">Control Flow</a><ul>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#compound-expressions">Compound Expressions</a></li>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#conditional-evaluation">Conditional Evaluation</a></li>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#short-circuit-evaluation">Short-Circuit Evaluation</a></li>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#repeated-evaluation-loops">Repeated Evaluation: Loops</a></li>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#exception-handling">Exception Handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#tasks-aka-coroutines">Tasks (aka Coroutines)</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="variables-and-scoping.html">Scope of Variables</a><ul>
<li class="toctree-l2"><a class="reference internal" href="variables-and-scoping.html#for-loops-and-comprehensions">For Loops and Comprehensions</a></li>
<li class="toctree-l2"><a class="reference internal" href="variables-and-scoping.html#constants">Constants</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="types.html">Types</a><ul>
<li class="toctree-l2"><a class="reference internal" href="types.html#type-declarations">Type Declarations</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#abstract-types">Abstract Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#bits-types">Bits Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#composite-types">Composite Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#immutable-composite-types">Immutable Composite Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#declared-types">Declared Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#tuple-types">Tuple Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#type-unions">Type Unions</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#man-parametric-types">Parametric Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#type-aliases">Type Aliases</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#operations-on-types">Operations on Types</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="methods.html">Methods</a><ul>
<li class="toctree-l2"><a class="reference internal" href="methods.html#defining-methods">Defining Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="methods.html#method-ambiguities">Method Ambiguities</a></li>
<li class="toctree-l2"><a class="reference internal" href="methods.html#parametric-methods">Parametric Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="methods.html#note-on-optional-and-keyword-arguments">Note on Optional and keyword Arguments</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="constructors.html">Constructors</a><ul>
<li class="toctree-l2"><a class="reference internal" href="constructors.html#outer-constructor-methods">Outer Constructor Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="constructors.html#inner-constructor-methods">Inner Constructor Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="constructors.html#incomplete-initialization">Incomplete Initialization</a></li>
<li class="toctree-l2"><a class="reference internal" href="constructors.html#parametric-constructors">Parametric Constructors</a></li>
<li class="toctree-l2"><a class="reference internal" href="constructors.html#case-study-rational">Case Study: Rational</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="conversion-and-promotion.html">Conversion and Promotion</a><ul>
<li class="toctree-l2"><a class="reference internal" href="conversion-and-promotion.html#conversion">Conversion</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversion-and-promotion.html#promotion">Promotion</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="modules.html">Modules</a><ul>
<li class="toctree-l2"><a class="reference internal" href="modules.html#summary-of-module-usage">Summary of module usage</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="metaprogramming.html">Metaprogramming</a><ul>
<li class="toctree-l2"><a class="reference internal" href="metaprogramming.html#expressions-and-eval">Expressions and Eval</a></li>
<li class="toctree-l2"><a class="reference internal" href="metaprogramming.html#macros">Macros</a></li>
<li class="toctree-l2"><a class="reference internal" href="metaprogramming.html#reflection">Reflection</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="arrays.html">Multi-dimensional Arrays</a><ul>
<li class="toctree-l2"><a class="reference internal" href="arrays.html#arrays">Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.html#sparse-matrices">Sparse Matrices</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="linear-algebra.html">Linear algebra</a><ul>
<li class="toctree-l2"><a class="reference internal" href="linear-algebra.html#matrix-factorizations">Matrix factorizations</a></li>
<li class="toctree-l2"><a class="reference internal" href="linear-algebra.html#special-matrices">Special matrices</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="networking-and-streams.html">Networking and Streams</a><ul>
<li class="toctree-l2"><a class="reference internal" href="networking-and-streams.html#basic-stream-i-o">Basic Stream I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="networking-and-streams.html#text-i-o">Text I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="networking-and-streams.html#working-with-files">Working with Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="networking-and-streams.html#a-simple-tcp-example">A simple TCP example</a></li>
<li class="toctree-l2"><a class="reference internal" href="networking-and-streams.html#resolving-ip-addresses">Resolving IP Addresses</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="parallel-computing.html">Parallel Computing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#data-movement">Data Movement</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#parallel-map-and-loops">Parallel Map and Loops</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#synchronization-with-remote-references">Synchronization With Remote References</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#scheduling">Scheduling</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#distributed-arrays">Distributed Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#constructing-distributed-arrays">Constructing Distributed Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#distributed-array-operations">Distributed Array Operations</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#shared-arrays-experimental">Shared Arrays (Experimental)</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#clustermanagers">ClusterManagers</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Running External Programs</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#interpolation">Interpolation</a></li>
<li class="toctree-l2"><a class="reference internal" href="#quoting">Quoting</a></li>
<li class="toctree-l2"><a class="reference internal" href="#pipelines">Pipelines</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="calling-c-and-fortran-code.html">Calling C and Fortran Code</a><ul>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#mapping-c-types-to-julia">Mapping C Types to Julia</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#accessing-data-through-a-pointer">Accessing Data through a Pointer</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#passing-pointers-for-modifying-inputs">Passing Pointers for Modifying Inputs</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#garbage-collection-safety">Garbage Collection Safety</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#non-constant-function-specifications">Non-constant Function Specifications</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#indirect-calls">Indirect Calls</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#calling-convention">Calling Convention</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#accessing-global-variables">Accessing Global Variables</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#passing-julia-callback-functions-to-c">Passing Julia Callback Functions to C</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#c">C++</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#handling-platform-variations">Handling Platform Variations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="interacting-with-julia.html">Interacting With Julia</a><ul>
<li class="toctree-l2"><a class="reference internal" href="interacting-with-julia.html#the-different-prompt-modes">The different prompt modes</a></li>
<li class="toctree-l2"><a class="reference internal" href="interacting-with-julia.html#key-bindings">Key bindings</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="embedding.html">Embedding Julia</a><ul>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#high-level-embedding">High-Level Embedding</a></li>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#converting-types">Converting Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#calling-julia-functions">Calling Julia Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#memory-management">Memory Management</a></li>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#working-with-arrays">Working with Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#exceptions">Exceptions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="packages.html">Packages</a><ul>
<li class="toctree-l2"><a class="reference internal" href="packages.html#package-status">Package Status</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#adding-and-removing-packages">Adding and Removing Packages</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#installing-unregistered-packages">Installing Unregistered Packages</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#updating-packages">Updating Packages</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#checkout-pin-and-free">Checkout, Pin and Free</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="packages.html#package-development">Package Development</a><ul>
<li class="toctree-l2"><a class="reference internal" href="packages.html#initial-setup">Initial Setup</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#generating-a-new-package">Generating a New Package</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#making-your-package-available">Making Your Package Available</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#publishing-your-package">Publishing Your Package</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#tagging-package-versions">Tagging Package Versions</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#fixing-package-requirements">Fixing Package Requirements</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#man-package-requirements">Requirements Specification</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="performance-tips.html">Performance Tips</a><ul>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#avoid-global-variables">Avoid global variables</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#measure-performance-with-time-and-pay-attention-to-memory-allocation">Measure performance with <tt class="docutils literal"><span class="pre">&#64;time</span></tt> and pay attention to memory allocation</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#tools">Tools</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#avoid-containers-with-abstract-type-parameters">Avoid containers with abstract type parameters</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#type-declarations">Type declarations</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#break-functions-into-multiple-definitions">Break functions into multiple definitions</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#write-type-stable-functions">Write &#8220;type-stable&#8221; functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#avoid-changing-the-type-of-a-variable">Avoid changing the type of a variable</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#separate-kernel-functions">Separate kernel functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#access-arrays-in-memory-order-along-columns">Access arrays in memory order, along columns</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#pre-allocating-outputs">Pre-allocating outputs</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#avoid-string-interpolation-for-i-o">Avoid string interpolation for I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#fix-deprecation-warnings">Fix deprecation warnings</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#tweaks">Tweaks</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#performance-annotations">Performance Annotations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="style-guide.html">Style Guide</a><ul>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#write-functions-not-just-scripts">Write functions, not just scripts</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#avoid-writing-overly-specific-types">Avoid writing overly-specific types</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#handle-excess-argument-diversity-in-the-caller">Handle excess argument diversity in the caller</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#append-to-names-of-functions-that-modify-their-arguments">Append <cite>!</cite> to names of functions that modify their arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#avoid-strange-type-unions">Avoid strange type Unions</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#try-to-avoid-nullable-fields">Try to avoid nullable fields</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#avoid-elaborate-container-types">Avoid elaborate container types</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#avoid-underscores-in-names">Avoid underscores in names</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-overuse-try-catch">Don&#8217;t overuse try-catch</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-parenthesize-conditions">Don&#8217;t parenthesize conditions</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-overuse">Don&#8217;t overuse ...</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-use-unnecessary-static-parameters">Don&#8217;t use unnecessary static parameters</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#avoid-confusion-about-whether-something-is-an-instance-or-a-type">Avoid confusion about whether something is an instance or a type</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-overuse-macros">Don&#8217;t overuse macros</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-expose-unsafe-operations-at-the-interface-level">Don&#8217;t expose unsafe operations at the interface level</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-overload-methods-of-base-container-types">Don&#8217;t overload methods of base container types</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#be-careful-with-type-equality">Be careful with type equality</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#do-not-write-x-f-x">Do not write <tt class="docutils literal"><span class="pre">x-&gt;f(x)</span></tt></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="faq.html#sessions-and-the-repl">Sessions and the REPL</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#functions">Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#types-type-declarations-and-constructors">Types, type declarations, and constructors</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#nothingness-and-missing-values">Nothingness and missing values</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#julia-releases">Julia Releases</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#developing-julia">Developing Julia</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="noteworthy-differences.html">Noteworthy Differences from other Languages</a><ul>
<li class="toctree-l2"><a class="reference internal" href="noteworthy-differences.html#noteworthy-differences-from-matlab">Noteworthy differences from MATLAB</a></li>
<li class="toctree-l2"><a class="reference internal" href="noteworthy-differences.html#noteworthy-differences-from-r">Noteworthy differences from R</a></li>
<li class="toctree-l2"><a class="reference internal" href="noteworthy-differences.html#noteworthy-differences-from-python">Noteworthy differences from Python</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/base.html">The Standard Library</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#introduction">Introduction</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#getting-around">Getting Around</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#all-objects">All Objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#types">Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#generic-functions">Generic Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#syntax">Syntax</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#iteration">Iteration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#general-collections">General Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#iterable-collections">Iterable Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#indexable-collections">Indexable Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#associative-collections">Associative Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#set-like-collections">Set-Like Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#dequeues">Dequeues</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#strings">Strings</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#i-o">I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#network-i-o">Network I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#text-i-o">Text I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#multimedia-i-o">Multimedia I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#memory-mapped-i-o">Memory-mapped I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#standard-numeric-types">Standard Numeric Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#mathematical-operators">Mathematical Operators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#mathematical-functions">Mathematical Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#data-formats">Data Formats</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#numbers">Numbers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#bigfloats">BigFloats</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#random-numbers">Random Numbers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#arrays">Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#combinatorics">Combinatorics</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#statistics">Statistics</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#signal-processing">Signal Processing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#numerical-integration">Numerical Integration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#parallel-computing">Parallel Computing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#distributed-arrays">Distributed Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#shared-arrays-experimental-unix-only-feature">Shared Arrays (Experimental, UNIX-only feature)</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#system">System</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#c-interface">C Interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#errors">Errors</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#tasks">Tasks</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#events">Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#reflection">Reflection</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#internals">Internals</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/sparse.html">Sparse Matrices</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/linalg.html">Linear Algebra</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/linalg.html#module-Base.LinAlg.BLAS">BLAS Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/constants.html">Constants</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/file.html">Filesystem</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/punctuation.html">Punctuation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/sort.html">Sorting and Related Functions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/sort.html#sorting-functions">Sorting Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/sort.html#order-related-functions">Order-Related Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/sort.html#sorting-algorithms">Sorting Algorithms</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/pkg.html">Package Manager Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/collections.html">Collections and Data Structures</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/collections.html#priorityqueue">PriorityQueue</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/collections.html#heap-functions">Heap Functions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/graphics.html">Graphics</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/graphics.html#geometry">Geometry</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/test.html">Unit and Functional Testing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/test.html#overview">Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/test.html#handlers">Handlers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/test.html#macros">Macros</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/test.html#functions">Functions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/test.html#testing-base-julia">Testing Base Julia</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/profile.html">Profiling</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/profile.html#basic-usage">Basic usage</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/profile.html#accumulation-and-clearing">Accumulation and clearing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/profile.html#options-for-controlling-the-display-of-profile-results">Options for controlling the display of profile results</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/profile.html#configuration">Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/profile.html#function-reference">Function reference</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../devdocs/julia.html">Documentation of Julia&#8217;s Internals</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../devdocs/cartesian.html">Base.Cartesian</a></li>
<li class="toctree-l2"><a class="reference internal" href="../devdocs/sysimg.html">System Image Building</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../devdocs/C.html">Developing/debugging Julia&#8217;s C code</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../devdocs/backtraces.html">Reporting and analyzing crashes (segfaults)</a></li>
<li class="toctree-l2"><a class="reference internal" href="../devdocs/debuggingtips.html">gdb debugging tips</a></li>
</ul>
</li>
</ul>

        
      </div>
      &nbsp;
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../index.html">Julia Language</a>
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../index.html">Docs</a> &raquo;</li>
      
    <li>Running External Programs</li>
      <li class="wy-breadcrumbs-aside">
        
          <a href="../_sources/manual/running-external-programs.txt" rel="nofollow"> View page source</a>
        
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document">
            
  <div class="section" id="running-external-programs">
<span id="man-running-external-programs"></span><h1>Running External Programs<a class="headerlink" href="#running-external-programs" title="Permalink to this headline">¶</a></h1>
<p>Julia borrows backtick notation for commands from the shell, Perl, and
Ruby. However, in Julia, writing</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="sb">`echo hello`</span>
<span class="sb">`echo hello`</span>
</pre></div>
</div>
<p>differs in several aspects from the behavior in various shells, Perl,
or Ruby:</p>
<ul class="simple">
<li>Instead of immediately running the command, backticks create a
<tt class="docutils literal"><span class="pre">Cmd</span></tt> object to represent the command. You can use this object to
connect the command to others via pipes, run it, and read or write to
it.</li>
<li>When the command is run, Julia does not capture its output unless you
specifically arrange for it to. Instead, the output of the command by
default goes to <tt class="docutils literal"><span class="pre">stdout</span></tt> as it would using <tt class="docutils literal"><span class="pre">libc</span></tt>&#8216;s <tt class="docutils literal"><span class="pre">system</span></tt>
call.</li>
<li>The command is never run with a shell. Instead, Julia parses the
command syntax directly, appropriately interpolating variables and
splitting on words as the shell would, respecting shell quoting
syntax. The command is run as <tt class="docutils literal"><span class="pre">julia</span></tt>&#8216;s immediate child process,
using <tt class="docutils literal"><span class="pre">fork</span></tt> and <tt class="docutils literal"><span class="pre">exec</span></tt> calls.</li>
</ul>
<p>Here&#8217;s a simple example of running an external program:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">run</span><span class="p">(</span><span class="sb">`echo hello`</span><span class="p">)</span>
<span class="n">hello</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">hello</span></tt> is the output of the <tt class="docutils literal"><span class="pre">echo</span></tt> command, sent to stdout.
The run method itself returns <tt class="docutils literal"><span class="pre">nothing</span></tt>, and throws an <tt class="docutils literal"><span class="pre">ErrorException</span></tt>
if the external command fails to run successfully.</p>
<p>If you want to read the output of the external command, the <tt class="docutils literal"><span class="pre">readall</span></tt> method
can be used instead:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">a</span><span class="o">=</span><span class="n">readall</span><span class="p">(</span><span class="sb">`echo hello`</span><span class="p">)</span>
<span class="s">&quot;hello</span><span class="se">\n</span><span class="s">&quot;</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="p">(</span><span class="n">chomp</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="o">==</span> <span class="s">&quot;hello&quot;</span>
<span class="n">true</span>
</pre></div>
</div>
<p>More generally, you can use <tt class="docutils literal"><span class="pre">open</span></tt> to read from or write to an external
command.  For example:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">open</span><span class="p">(</span><span class="sb">`less`</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">,</span> <span class="n">STDOUT</span><span class="p">)</span> <span class="k">do</span> <span class="n">io</span>
           <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="mi">1000</span>
               <span class="n">println</span><span class="p">(</span><span class="n">io</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
           <span class="k">end</span>
       <span class="k">end</span>
</pre></div>
</div>
<div class="section" id="interpolation">
<span id="man-command-interpolation"></span><h2>Interpolation<a class="headerlink" href="#interpolation" title="Permalink to this headline">¶</a></h2>
<p>Suppose you want to do something a bit more complicated and use the name
of a file in the variable <tt class="docutils literal"><span class="pre">file</span></tt> as an argument to a command. You can
use <tt class="docutils literal"><span class="pre">$</span></tt> for interpolation much as you would in a string literal (see
<a class="reference internal" href="strings.html#man-strings"><em>Strings</em></a>):</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">file</span> <span class="o">=</span> <span class="s">&quot;/etc/passwd&quot;</span>
<span class="s">&quot;/etc/passwd&quot;</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="sb">`sort $file`</span>
<span class="sb">`sort /etc/passwd`</span>
</pre></div>
</div>
<p>A common pitfall when running external programs via a shell is that if a
file name contains characters that are special to the shell, they may
cause undesirable behavior. Suppose, for example, rather than
<tt class="docutils literal"><span class="pre">/etc/passwd</span></tt>, we wanted to sort the contents of the file
<tt class="docutils literal"><span class="pre">/Volumes/External</span> <span class="pre">HD/data.csv</span></tt>. Let&#8217;s try it:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">file</span> <span class="o">=</span> <span class="s">&quot;/Volumes/External HD/data.csv&quot;</span>
<span class="s">&quot;/Volumes/External HD/data.csv&quot;</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="sb">`sort $file`</span>
<span class="sb">`sort &#39;/Volumes/External HD/data.csv&#39;`</span>
</pre></div>
</div>
<p>How did the file name get quoted? Julia knows that <tt class="docutils literal"><span class="pre">file</span></tt> is meant to
be interpolated as a single argument, so it quotes the word for you.
Actually, that is not quite accurate: the value of <tt class="docutils literal"><span class="pre">file</span></tt> is never
interpreted by a shell, so there&#8217;s no need for actual quoting; the
quotes are inserted only for presentation to the user. This will even
work if you interpolate a value as part of a shell word:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">path</span> <span class="o">=</span> <span class="s">&quot;/Volumes/External HD&quot;</span>
<span class="s">&quot;/Volumes/External HD&quot;</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">name</span> <span class="o">=</span> <span class="s">&quot;data&quot;</span>
<span class="s">&quot;data&quot;</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">ext</span> <span class="o">=</span> <span class="s">&quot;csv&quot;</span>
<span class="s">&quot;csv&quot;</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="sb">`sort $path/$name.$ext`</span>
<span class="sb">`sort &#39;/Volumes/External HD/data.csv&#39;`</span>
</pre></div>
</div>
<p>As you can see, the space in the <tt class="docutils literal"><span class="pre">path</span></tt> variable is appropriately
escaped. But what if you <em>want</em> to interpolate multiple words? In that
case, just use an array (or any other iterable container):</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">files</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;/etc/passwd&quot;</span><span class="p">,</span><span class="s">&quot;/Volumes/External HD/data.csv&quot;</span><span class="p">]</span>
<span class="mi">2</span><span class="o">-</span><span class="n">element</span> <span class="n">ASCIIString</span> <span class="n">Array</span><span class="p">:</span>
 <span class="s">&quot;/etc/passwd&quot;</span>
 <span class="s">&quot;/Volumes/External HD/data.csv&quot;</span>


<span class="n">julia</span><span class="o">&gt;</span> <span class="sb">`grep foo $files`</span>
<span class="sb">`grep foo /etc/passwd &#39;/Volumes/External HD/data.csv&#39;`</span>
</pre></div>
</div>
<p>If you interpolate an array as part of a shell word, Julia emulates the
shell&#8217;s <tt class="docutils literal"><span class="pre">{a,b,c}</span></tt> argument generation:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;foo&quot;</span><span class="p">,</span><span class="s">&quot;bar&quot;</span><span class="p">,</span><span class="s">&quot;baz&quot;</span><span class="p">]</span>
<span class="mi">3</span><span class="o">-</span><span class="n">element</span> <span class="n">ASCIIString</span> <span class="n">Array</span><span class="p">:</span>
 <span class="s">&quot;foo&quot;</span>
 <span class="s">&quot;bar&quot;</span>
 <span class="s">&quot;baz&quot;</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="sb">`grep xylophone $names.txt`</span>
<span class="sb">`grep xylophone foo.txt bar.txt baz.txt`</span>
</pre></div>
</div>
<p>Moreover, if you interpolate multiple arrays into the same word, the
shell&#8217;s Cartesian product generation behavior is emulated:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;foo&quot;</span><span class="p">,</span><span class="s">&quot;bar&quot;</span><span class="p">,</span><span class="s">&quot;baz&quot;</span><span class="p">]</span>
<span class="mi">3</span><span class="o">-</span><span class="n">element</span> <span class="n">ASCIIString</span> <span class="n">Array</span><span class="p">:</span>
 <span class="s">&quot;foo&quot;</span>
 <span class="s">&quot;bar&quot;</span>
 <span class="s">&quot;baz&quot;</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">exts</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;aux&quot;</span><span class="p">,</span><span class="s">&quot;log&quot;</span><span class="p">]</span>
<span class="mi">2</span><span class="o">-</span><span class="n">element</span> <span class="n">ASCIIString</span> <span class="n">Array</span><span class="p">:</span>
 <span class="s">&quot;aux&quot;</span>
 <span class="s">&quot;log&quot;</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="sb">`rm -f $names.$exts`</span>
<span class="sb">`rm -f foo.aux foo.log bar.aux bar.log baz.aux baz.log`</span>
</pre></div>
</div>
<p>Since you can interpolate literal arrays, you can use this generative
functionality without needing to create temporary array objects first:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="sb">`rm -rf $[&quot;foo&quot;,&quot;bar&quot;,&quot;baz&quot;,&quot;qux&quot;].$[&quot;aux&quot;,&quot;log&quot;,&quot;pdf&quot;]`</span>
<span class="sb">`rm -rf foo.aux foo.log foo.pdf bar.aux bar.log bar.pdf baz.aux baz.log baz.pdf qux.aux qux.log qux.pdf`</span>
</pre></div>
</div>
</div>
<div class="section" id="quoting">
<h2>Quoting<a class="headerlink" href="#quoting" title="Permalink to this headline">¶</a></h2>
<p>Inevitably, one wants to write commands that aren&#8217;t quite so simple, and
it becomes necessary to use quotes. Here&#8217;s a simple example of a perl
one-liner at a shell prompt:</p>
<div class="highlight-julia"><pre>sh$ perl -le '$|=1; for (0..3) { print }'
0
1
2
3</pre>
</div>
<p>The Perl expression needs to be in single quotes for two reasons: so
that spaces don&#8217;t break the expression into multiple shell words, and so
that uses of Perl variables like <tt class="docutils literal"><span class="pre">$|</span></tt> (yes, that&#8217;s the name of a
variable in Perl), don&#8217;t cause interpolation. In other instances, you
may want to use double quotes so that interpolation <em>does</em> occur:</p>
<div class="highlight-julia"><pre>sh$ first="A"
sh$ second="B"
sh$ perl -le '$|=1; print for @ARGV' "1: $first" "2: $second"
1: A
2: B</pre>
</div>
<p>In general, the Julia backtick syntax is carefully designed so that you
can just cut-and-paste shell commands as-is into backticks and they will
work: the escaping, quoting, and interpolation behaviors are the same as
the shell&#8217;s. The only difference is that the interpolation is integrated
and aware of Julia&#8217;s notion of what is a single string value, and what
is a container for multiple values. Let&#8217;s try the above two examples in
Julia:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="sb">`perl -le &#39;$|=1; for (0..3) { print }&#39;`</span>
<span class="sb">`perl -le &#39;$|=1; for (0..3) { print }&#39;`</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">run</span><span class="p">(</span><span class="n">ans</span><span class="p">)</span>
<span class="mi">0</span>
<span class="mi">1</span>
<span class="mi">2</span>
<span class="mi">3</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">first</span> <span class="o">=</span> <span class="s">&quot;A&quot;</span><span class="p">;</span> <span class="n">second</span> <span class="o">=</span> <span class="s">&quot;B&quot;</span><span class="p">;</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="sb">`perl -le &#39;print for @ARGV&#39; &quot;1: $first&quot; &quot;2: $second&quot;`</span>
<span class="sb">`perl -le &#39;print for @ARGV&#39; &#39;1: A&#39; &#39;2: B&#39;`</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">run</span><span class="p">(</span><span class="n">ans</span><span class="p">)</span>
<span class="mi">1</span><span class="p">:</span> <span class="n">A</span>
<span class="mi">2</span><span class="p">:</span> <span class="n">B</span>
</pre></div>
</div>
<p>The results are identical, and Julia&#8217;s interpolation behavior mimics the
shell&#8217;s with some improvements due to the fact that Julia supports
first-class iterable objects while most shells use strings split on
spaces for this, which introduces ambiguities. When trying to port shell
commands to Julia, try cut and pasting first. Since Julia shows commands
to you before running them, you can easily and safely just examine its
interpretation without doing any damage.</p>
</div>
<div class="section" id="pipelines">
<h2>Pipelines<a class="headerlink" href="#pipelines" title="Permalink to this headline">¶</a></h2>
<p>Shell metacharacters, such as <tt class="docutils literal"><span class="pre">|</span></tt>, <tt class="docutils literal"><span class="pre">&amp;</span></tt>, and <tt class="docutils literal"><span class="pre">&gt;</span></tt>, are not special
inside of Julia&#8217;s backticks: unlike in the shell, inside of Julia&#8217;s
backticks, a pipe is always just a pipe:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">run</span><span class="p">(</span><span class="sb">`echo hello | sort`</span><span class="p">)</span>
<span class="n">hello</span> <span class="o">|</span> <span class="n">sort</span>
</pre></div>
</div>
<p>This expression invokes the <tt class="docutils literal"><span class="pre">echo</span></tt> command with three words as
arguments: &#8220;hello&#8221;, &#8220;|&#8221;, and &#8220;sort&#8221;. The result is that a single line
is printed: &#8220;hello | sort&#8221;. Inside of backticks, a &#8220;|&#8221; is just a
literal pipe character. How, then, does one construct a pipeline?
Instead of using &#8220;|&#8221; inside of backticks, one uses Julia&#8217;s <tt class="docutils literal"><span class="pre">|&gt;</span></tt>
operator between <tt class="docutils literal"><span class="pre">Cmd</span></tt> objects:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">run</span><span class="p">(</span><span class="sb">`echo hello`</span> <span class="o">|&gt;</span> <span class="sb">`sort`</span><span class="p">)</span>
<span class="n">hello</span>
</pre></div>
</div>
<p>This pipes the output of the <tt class="docutils literal"><span class="pre">echo</span></tt> command to the <tt class="docutils literal"><span class="pre">sort</span></tt> command.
Of course, this isn&#8217;t terribly interesting since there&#8217;s only one line
to sort, but we can certainly do much more interesting things:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">run</span><span class="p">(</span><span class="sb">`cut -d: -f3 /etc/passwd`</span> <span class="o">|&gt;</span> <span class="sb">`sort -n`</span> <span class="o">|&gt;</span> <span class="sb">`tail -n5`</span><span class="p">)</span>
<span class="mi">210</span>
<span class="mi">211</span>
<span class="mi">212</span>
<span class="mi">213</span>
<span class="mi">214</span>
</pre></div>
</div>
<p>This prints the highest five user IDs on a UNIX system. The <tt class="docutils literal"><span class="pre">cut</span></tt>,
<tt class="docutils literal"><span class="pre">sort</span></tt> and <tt class="docutils literal"><span class="pre">tail</span></tt> commands are all spawned as immediate children of
the current <tt class="docutils literal"><span class="pre">julia</span></tt> process, with no intervening shell process. Julia
itself does the work to setup pipes and connect file descriptors that is
normally done by the shell. Since Julia does this itself, it retains
better control and can do some things that shells cannot.</p>
<p>Julia can run multiple commands in parallel:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">run</span><span class="p">(</span><span class="sb">`echo hello`</span> <span class="o">&amp;</span> <span class="sb">`echo world`</span><span class="p">)</span>
<span class="n">world</span>
<span class="n">hello</span>
</pre></div>
</div>
<p>The order of the output here is non-deterministic because the two
<tt class="docutils literal"><span class="pre">echo</span></tt> processes are started nearly simultaneously, and race to make
the first write to the <tt class="docutils literal"><span class="pre">stdout</span></tt> descriptor they share with each other
and the <tt class="docutils literal"><span class="pre">julia</span></tt> parent process. Julia lets you pipe the output from
both of these processes to another program:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">run</span><span class="p">(</span><span class="sb">`echo world`</span> <span class="o">&amp;</span> <span class="sb">`echo hello`</span> <span class="o">|&gt;</span> <span class="sb">`sort`</span><span class="p">)</span>
<span class="n">hello</span>
<span class="n">world</span>
</pre></div>
</div>
<p>In terms of UNIX plumbing, what&#8217;s happening here is that a single UNIX
pipe object is created and written to by both <tt class="docutils literal"><span class="pre">echo</span></tt> processes, and
the other end of the pipe is read from by the <tt class="docutils literal"><span class="pre">sort</span></tt> command.</p>
<p>The combination of a high-level programming language, a first-class
command abstraction, and automatic setup of pipes between processes is a
powerful one. To give some sense of the complex pipelines that can be
created easily, here are some more sophisticated examples, with
apologies for the excessive use of Perl one-liners:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">prefixer</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">sleep</span><span class="p">)</span> <span class="o">=</span> <span class="sb">`perl -nle &#39;$|=1; print &quot;&#39;$prefix&#39; &quot;, $_; sleep &#39;$sleep&#39;;&#39;`</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">run</span><span class="p">(</span><span class="sb">`perl -le &#39;$|=1; for(0..9){ print; sleep 1 }&#39;`</span> <span class="o">|&gt;</span> <span class="n">prefixer</span><span class="p">(</span><span class="s">&quot;A&quot;</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">prefixer</span><span class="p">(</span><span class="s">&quot;B&quot;</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span>
<span class="n">A</span>   <span class="mi">0</span>
<span class="n">B</span>   <span class="mi">1</span>
<span class="n">A</span>   <span class="mi">2</span>
<span class="n">B</span>   <span class="mi">3</span>
<span class="n">A</span>   <span class="mi">4</span>
<span class="n">B</span>   <span class="mi">5</span>
<span class="n">A</span>   <span class="mi">6</span>
<span class="n">B</span>   <span class="mi">7</span>
<span class="n">A</span>   <span class="mi">8</span>
<span class="n">B</span>   <span class="mi">9</span>
</pre></div>
</div>
<p>This is a classic example of a single producer feeding two concurrent
consumers: one <tt class="docutils literal"><span class="pre">perl</span></tt> process generates lines with the numbers 0
through 9 on them, while two parallel processes consume that output, one
prefixing lines with the letter &#8220;A&#8221;, the other with the letter &#8220;B&#8221;.
Which consumer gets the first line is non-deterministic, but once that
race has been won, the lines are consumed alternately by one process and
then the other. (Setting <tt class="docutils literal"><span class="pre">$|=1</span></tt> in Perl causes each print statement to
flush the <tt class="docutils literal"><span class="pre">stdout</span></tt> handle, which is necessary for this example to
work. Otherwise all the output is buffered and printed to the pipe at
once, to be read by just one consumer process.)</p>
<p>Here is an even more complex multi-stage producer-consumer example:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">run</span><span class="p">(</span><span class="sb">`perl -le &#39;$|=1; for(0..9){ print; sleep 1 }&#39;`</span> <span class="o">|&gt;</span>
           <span class="n">prefixer</span><span class="p">(</span><span class="s">&quot;X&quot;</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">prefixer</span><span class="p">(</span><span class="s">&quot;Y&quot;</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">prefixer</span><span class="p">(</span><span class="s">&quot;Z&quot;</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">|&gt;</span>
           <span class="n">prefixer</span><span class="p">(</span><span class="s">&quot;A&quot;</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">prefixer</span><span class="p">(</span><span class="s">&quot;B&quot;</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span>
<span class="n">B</span>   <span class="n">Y</span>   <span class="mi">0</span>
<span class="n">A</span>   <span class="n">Z</span>   <span class="mi">1</span>
<span class="n">B</span>   <span class="n">X</span>   <span class="mi">2</span>
<span class="n">A</span>   <span class="n">Y</span>   <span class="mi">3</span>
<span class="n">B</span>   <span class="n">Z</span>   <span class="mi">4</span>
<span class="n">A</span>   <span class="n">X</span>   <span class="mi">5</span>
<span class="n">B</span>   <span class="n">Y</span>   <span class="mi">6</span>
<span class="n">A</span>   <span class="n">Z</span>   <span class="mi">7</span>
<span class="n">B</span>   <span class="n">X</span>   <span class="mi">8</span>
<span class="n">A</span>   <span class="n">Y</span>   <span class="mi">9</span>
</pre></div>
</div>
<p>This example is similar to the previous one, except there are two stages
of consumers, and the stages have different latency so they use a
different number of parallel workers, to maintain saturated throughput.</p>
<p>We strongly encourage you to try all these examples to see how they work.</p>
</div>
</div>


          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="calling-c-and-fortran-code.html" class="btn btn-neutral float-right" title="Calling C and Fortran Code"/>Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="parallel-computing.html" class="btn btn-neutral" title="Parallel Computing"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
    </p>
  </div>

  <a href="https://github.com/snide/sphinx_rtd_theme">Sphinx theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>
</footer>
        </div>
      </div>

    </section>

  </div>
  

  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../',
            VERSION:'0.3.4',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true
        };
    </script>
      <script type="text/javascript" src="../_static/jquery.js"></script>
      <script type="text/javascript" src="../_static/underscore.js"></script>
      <script type="text/javascript" src="../_static/doctools.js"></script>
      <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>