Shell.@esc_cmdMacro
@esc_str -> String

Help you escape special characters for the shell.

Examples

julia> files = ["temp file 1", "temp file 2"]
2-element Array{String,1}:
 "temp file 1"
 "temp file 2"

julia> filelist = esc`$files.txt`
"'temp file 1.txt' 'temp file 2.txt'"

julia> Shell.run("touch $filelist")

julia> Shell.run("rm $filelist")

Be careful, the escape treat space separated terms individually. Put them into a varible to get properly escaped.

Examples

julia> esc`temp file 0.txt`
"temp file 0.txt"

julia> file = "temp file 0.txt"
"temp file 0.txt"

julia> esc`$file`
"'temp file 0.txt'"
  • Not working for cmd in Windows because it treats single quotes differently.
Shell.runMethod
run(cmd::AbstractString; shell=SHELL, capture=CAPTURE, chomp=CHOMP,
    dryrun=DRYRUN, source=SOURCE)

Run your command string in shell.

Examples

julia> using Shell

julia> Shell.run(raw"echo $SHELL", capture=true)
"/bin/zsh"

julia> Shell.run(raw"for i in bu fan; do echo $i; done")
bu
fan

julia> files = ["temp file 1", "temp file 2"]
2-element Array{String,1}:
 "temp file 1"
 "temp file 2"

julia> filelist = esc`$files.txt`
"'temp file 1.txt' 'temp file 2.txt'"

julia> Shell.run("touch $filelist")

julia> Shell.run("ls > 'temp file 0.txt'")

julia> Shell.run("cat 'temp file 0.txt' | grep temp")
temp file 0.txt
temp file 1.txt
temp file 2.txt

julia> Shell.run("rm 'temp file'*")
  • You should properly escape all special characters manually.
  • use dryrun=true to check the command to be run without actually running.
  • To capture output, set capture=true.
  • To avoid escaping $ everytime, you can use raw string, like raw"echo $PATH"
  • You can change the default shell (zsh in linux and cmd in windows) using setshell("other_shell").
  • In Windows, shell should be cmd or powershell.
Shell.setchompMethod
setchomp(chomp::Bool)

Set whether chomp the output (default is true).

Shell.setiscaptureMethod
setiscapture(capture::Bool)

Whether capture the rc file (e.g. .zshrc) before run script (default is true).

Shell.setisdryrunMethod
setisdryrun(dryrun::Bool)

Whether dryrun the rc file (e.g. .zshrc) before run script (default is true).

Shell.setissourceMethod
setissource(source::Bool)

Whether source the rc file (e.g. .zshrc) before run script (default is true).

Shell.setshellMethod
setshell(shell::AbstractString)

Specify which shell to use (Windows defaults to cmd, and zsh otherwise).