3
3
Julia borrows backtick notation for commands from the shell, Perl, and Ruby. However, in Julia,
4
4
writing
5
5
6
- ``` julia
6
+ ``` jldoctest
7
7
julia> `echo hello`
8
8
`echo hello`
9
9
```
@@ -22,8 +22,14 @@ differs in several aspects from the behavior in various shells, Perl, or Ruby:
22
22
23
23
Here's a simple example of running an external program:
24
24
25
- ``` julia
26
- julia> run (` echo hello` )
25
+ ``` jldoctest
26
+ julia> mycommand = `echo hello`
27
+ `echo hello`
28
+
29
+ julia> typeof(mycommand)
30
+ Cmd
31
+
32
+ julia> run(mycommand)
27
33
hello
28
34
```
29
35
@@ -33,11 +39,11 @@ successfully.
33
39
34
40
If you want to read the output of the external command, [ ` readstring() ` ] ( @ref ) can be used instead:
35
41
36
- ``` julia
37
- julia> a= readstring (` echo hello` )
42
+ ``` jldoctest
43
+ julia> a = readstring(`echo hello`)
38
44
"hello\n"
39
45
40
- julia> ( chomp (a) ) == " hello"
46
+ julia> chomp(a) == "hello"
41
47
true
42
48
```
43
49
@@ -60,7 +66,7 @@ Suppose you want to do something a bit more complicated and use the name of a fi
60
66
` file ` as an argument to a command. You can use ` $ ` for interpolation much as you would in a string
61
67
literal (see [ Strings] ( @ref ) ):
62
68
63
- ``` julia
69
+ ``` jldoctest
64
70
julia> file = "/etc/passwd"
65
71
"/etc/passwd"
66
72
@@ -73,7 +79,7 @@ that are special to the shell, they may cause undesirable behavior. Suppose, for
73
79
than ` /etc/passwd ` , we wanted to sort the contents of the file ` /Volumes/External HD/data.csv ` .
74
80
Let's try it:
75
81
76
- ``` julia
82
+ ``` jldoctest
77
83
julia> file = "/Volumes/External HD/data.csv"
78
84
"/Volumes/External HD/data.csv"
79
85
@@ -87,7 +93,7 @@ is never interpreted by a shell, so there's no need for actual quoting; the quot
87
93
only for presentation to the user. This will even work if you interpolate a value as part of a
88
94
shell word:
89
95
90
- ``` julia
96
+ ``` jldoctest
91
97
julia> path = "/Volumes/External HD"
92
98
"/Volumes/External HD"
93
99
@@ -104,7 +110,7 @@ julia> `sort $path/$name.$ext`
104
110
As you can see, the space in the ` path ` variable is appropriately escaped. But what if you * want*
105
111
to interpolate multiple words? In that case, just use an array (or any other iterable container):
106
112
107
- ``` julia
113
+ ``` jldoctest
108
114
julia> files = ["/etc/passwd","/Volumes/External HD/data.csv"]
109
115
2-element Array{String,1}:
110
116
"/etc/passwd"
@@ -117,7 +123,7 @@ julia> `grep foo $files`
117
123
If you interpolate an array as part of a shell word, Julia emulates the shell's ` {a,b,c} ` argument
118
124
generation:
119
125
120
- ``` julia
126
+ ``` jldoctest
121
127
julia> names = ["foo","bar","baz"]
122
128
3-element Array{String,1}:
123
129
"foo"
@@ -131,7 +137,7 @@ julia> `grep xylophone $names.txt`
131
137
Moreover, if you interpolate multiple arrays into the same word, the shell's Cartesian product
132
138
generation behavior is emulated:
133
139
134
- ``` julia
140
+ ``` jldoctest
135
141
julia> names = ["foo","bar","baz"]
136
142
3-element Array{String,1}:
137
143
"foo"
@@ -150,7 +156,7 @@ julia> `rm -f $names.$exts`
150
156
Since you can interpolate literal arrays, you can use this generative functionality without needing
151
157
to create temporary array objects first:
152
158
153
- ``` julia
159
+ ``` jldoctest
154
160
julia> `rm -rf $["foo","bar","baz","qux"].$["aux","log","pdf"]`
155
161
`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`
156
162
```
@@ -187,22 +193,22 @@ behaviors are the same as the shell's. The only difference is that the interpola
187
193
and aware of Julia's notion of what is a single string value, and what is a container for multiple
188
194
values. Let's try the above two examples in Julia:
189
195
190
- ``` julia
191
- julia> ` perl -le '$|=1; for (0..3) { print }'`
196
+ ``` jldoctest
197
+ julia> A = `perl -le '$|=1; for (0..3) { print }'`
192
198
`perl -le '$|=1; for (0..3) { print }'`
193
199
194
- julia> run (ans )
200
+ julia> run(A )
195
201
0
196
202
1
197
203
2
198
204
3
199
205
200
206
julia> first = "A"; second = "B";
201
207
202
- julia> ` perl -le 'print for @ARGV' "1: $first " "2: $second "`
208
+ julia> B = `perl -le 'print for @ARGV' "1: $first" "2: $second"`
203
209
`perl -le 'print for @ARGV' '1: A' '2: B'`
204
210
205
- julia> run (ans )
211
+ julia> run(B )
206
212
1: A
207
213
2: B
208
214
```
@@ -215,20 +221,21 @@ easily and safely just examine its interpretation without doing any damage.
215
221
216
222
## Pipelines
217
223
218
- Shell metacharacters, such as ` | ` , ` & ` , and ` > ` , are not special inside of Julia's backticks:
219
- unlike in the shell, inside of Julia's backticks, a pipe is always just a pipe:
224
+ Shell metacharacters, such as ` | ` , ` & ` , and ` > ` , need to be quoted (or escaped) inside of Julia's backticks:
220
225
221
- ``` julia
222
- julia> run (` echo hello | sort` )
226
+ ``` jldoctest
227
+ julia> run(`echo hello '|' sort`)
228
+ hello | sort
229
+
230
+ julia> run(`echo hello \| sort`)
223
231
hello | sort
224
232
```
225
233
226
- This expression invokes the ` echo ` command with three words as arguments: "hello", "|", and "sort".
227
- The result is that a single line is printed: "hello | sort". Inside of backticks, a "|" is just
228
- a literal pipe character. How, then, does one construct a pipeline? Instead of using "|" inside
229
- of backticks, one uses [ ` pipeline() ` ] ( @ref ) :
234
+ This expression invokes the ` echo ` command with three words as arguments: ` hello ` , ` | ` , and ` sort ` .
235
+ The result is that a single line is printed: ` hello | sort ` . How, then, does one construct a
236
+ pipeline? Instead of using ` '|' ` inside of backticks, one uses [ ` pipeline() ` ] ( @ref ) :
230
237
231
- ``` julia
238
+ ``` jldoctest
232
239
julia> run(pipeline(`echo hello`, `sort`))
233
240
hello
234
241
```
@@ -265,7 +272,7 @@ nearly simultaneously, and race to make the first write to the [`STDOUT`](@ref)
265
272
share with each other and the ` julia ` parent process. Julia lets you pipe the output from both
266
273
of these processes to another program:
267
274
268
- ``` julia
275
+ ``` jldoctest
269
276
julia> run(pipeline(`echo world` & `echo hello`, `sort`))
270
277
hello
271
278
world
0 commit comments