Running shell commands
To interact with the operating system from within the Julia REPL, there are a few helper functions available, as follows:
pwd()
: this function prints the current directory, for example,"d:\\test"
cd("d:\\test\\week1")
: this function helps to navigate to subdirectories;
: in the interactive shell, you can also use shell mode using the;
modifier, for example:; cd folder
: navigates tofolder
However, what if you want to run a shell command by using the operating system (the OS)? Julia offers efficient shell integration through the run
function, which takes an object of type Cmd
, defined by enclosing a command string in backticks (``
).
The following are some examples for Linux or macOS X (at the time of writing: September 2018):
# Code in Chapter 9\shell.jl: cmd = `echo Julia is smart` typeof(cmd) #> Cmd run(cmd) # returns Julia is smart run(`date`) #> Sat Jul 14 09:44:50 GMT 2018 cmd = `cat file1.txt` run(cmd) # prints the contents of file1.txt...