# How to translate bash to .bat

**URL:** <https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 25, 2022, 1:01pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652 "2022-05-25T13:01:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 25, 2022, 1:01pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/1 "2022-05-25T13:01:41Z")

</div>

I have a bash script to create a Julia system image. ([KiteSimulators.jl/create\_sys\_image at main · aenarete/KiteSimulators.jl · GitHub](https://github.com/aenarete/KiteSimulators.jl/blob/main/bin/create_sys_image))  
Now I want to convert it to a .bat file for windows users. How could I translate these lines:

```julia
julia_version=$(julia --version | awk '{print($3)}')
julia_major=${julia_version:0:3} 
if test -f "kps-image-${julia_major}.so"; then
    mv bin/kps-image-${julia_major}.so kps-image-${julia_major}.so.bak
fi

```

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [May 25, 2022, 1:05pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/2 "2022-05-25T13:05:04Z")

</div>

Maybe translating to powershell could be easier. I can’t help with either but I’ve heard nice things about it.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 25, 2022, 1:24pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/3 "2022-05-25T13:24:12Z")

</div>

A problem here. `julia --version` on Windows opens the Julia REPL itself instead of printing the version info.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 25, 2022, 1:25pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/4 "2022-05-25T13:25:21Z")

</div>

It definitely does not.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 25, 2022, 1:28pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/5 "2022-05-25T13:28:38Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/9/7/976b93a0f6b083c3a6cb13a8a54cf8c07d94449d.png)

then maybe it’s a 1.8beta bug (the one I happen to have in my path)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 25, 2022, 1:29pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/6 "2022-05-25T13:29:43Z")

</div>

Works for me (WezTerm running cmd.exe):

```julia
C:\Users\ngudat>julia --version

julia version 1.7.2

C:\Users\ngudat>juliabeta --version

julia version 1.8.0-beta3

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 25, 2022, 1:33pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/7 "2022-05-25T13:33:13Z")

</div>

OK, found out why. I’m calling Julia through a batch file. If I call the exe directly then yes, it prints the version number.

```julia
C:\Users\joaqu>C:\programs\julia-1.8\bin\julia.exe --version
julia version 1.8.0-beta3

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 25, 2022, 1:38pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/8 "2022-05-25T13:38:08Z")

</div>

Already found out how to extract the version number from the version string: [How to extract part of a string in Windows batch file? - Super User](https://superuser.com/questions/228794/how-to-extract-part-of-a-string-in-windows-batch-file)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 25, 2022, 1:45pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/9 "2022-05-25T13:45:20Z")

</div>

I’m calling Julia through a batch file as well (that’s what a `.bat` is, right?)

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 25, 2022, 1:56pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/10 "2022-05-25T13:56:10Z")

</div>

Yes.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/9/9991170091713bbe4110e7e5bc09671f944ff2c5.png)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 25, 2022, 1:56pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/11 "2022-05-25T13:56:33Z")

</div>

Yes. I have two scripts, one that creates the system image, and one the starts Julia with the correct parameter to make use of the system image.

But because the system image works only with a specific Julia version I include the version number in the image name. In this way I can switch between different Julia versions and it always loads the correct system image.

I even found a way to test my .bat file on Linux:

```julia
wine cmd

```

Very happy 🙂

Is it possible to install Julia in wine?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 25, 2022, 2:15pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/12 "2022-05-25T14:15:59Z")

</div>

Is it the content of the bat file then? Mine is:

```julia
@echo off
echo.
C:\Users\ngudat\AppData\Local\Programs\Julia-1.7.2\bin\julia.exe %*

```

(I have no idea how any of this works, just copy pasted from somewhere on the internet - I believe the `%*` at the end might be responsible for passing along arguments?)

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 25, 2022, 2:19pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/13 "2022-05-25T14:19:31Z")

</div>

Yes, likely a lot works, but not all (just keep in mind even, if with superficial testing works, always a possibility not all implemented/tested):

[https://github.com/JuliaLang/julia/issues/43566](https://github.com/JuliaLang/julia/issues/43566)

Note, Julialang has a Wine label, so they want it to work, but as you can see it’s a problem with (upstream) wine, not Julia. It would always be a problem with wine, as it’s responsible for the conversion from Windows API.

Conversely, Linux Julia builds work in WSL [2], and again there might be bugs in Windows WSL, but I find it more likely to work, since they’re not trying to support a huge kernel API, that is a moving target, or rather it is, but they have the Linux kernel so not reimplementing from scratch. I understand Julia under WSL can sometimes be faster tan the native Windows Julia build (but that’s a problem with the Windows API not Julia).

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 25, 2022, 2:33pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/14 "2022-05-25T14:33:57Z")

</div>

I’ve been copying mine version-after-version and some (all?) of it is no longer necessary but it was at a certain time. This oi what I have

```julia
@ echo off

call C:\programs\WPy64-31020\env2.bat

set pato=C:\programs\ImageMagick;C:\MinGW64\msys64\usr\bin;C:\MinGW64\mingw64\bin;
set path=%pato%;%path%

C:\programs\julia-1.8\bin\julia.exe --sysimage-native-code=yes

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 25, 2022, 2:41pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/15 "2022-05-25T14:41:47Z")

</div>

> [@nilshg](#):
>
> (I have no idea how any of this works, just copy pasted from somewhere on the internet - I believe the `%*` at the end might be responsible for passing along arguments?)

Indeed. Adding that `%*` to mine also prints the version number.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 25, 2022, 3:42pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/16 "2022-05-25T15:42:07Z")

</div>

Finished my translation (fully tested, but only works if your package is registered):

```nohighlight
@echo off
REM This script creates a julia system image with a name that
REM contains the Julia version, e.g. kps-image-1.7.dll
REM It should be placed in the bin folder of the project.
REM It assumes that the following files exist:
REM test/update_packages.jl
REM test/create_sys_image.jl

REM determine basename of current directory
for /F "delims=" %%i in ("%cd%") do set basename=%%~ni

if %basename%==bin cd ..
for /f "delims=" %%i in ('julia --version') do set version_string=%%i
for /f "tokens=3 delims= " %%a in ("%version_string%") do set version=%%a

set julia_major=%version:~0,3%
set image=kps-image-%julia_major%.dll

IF EXIST "bin/%image%" (
  move bin/kps-image-%julia_major%.dll bin/kps-image-%julia_major%.so.bak
)

echo Updating packages...
if EXIST "Manifest.toml" (
    move Manifest.toml Manifest.toml.bak
)

julia --project -e "include(\"./test/update_packages.jl\");"
julia --project -e "using Pkg; Pkg.precompile()"
julia --project -e "include(\"./test/create_sys_image.jl\");"
move kps-image_tmp.so bin/kps-image-%julia_major%.dll
julia --project -e "using Pkg; Pkg.precompile()"

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 25, 2022, 4:04pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/17 "2022-05-25T16:04:17Z")

</div>

> [@ufechner7](#):
>
> I have a bash script to create a Julia system image

Why not write the script in Julia? Then it can be portable to Windows, and you don’t need a second (or third) language.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 25, 2022, 4:12pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/18 "2022-05-25T16:12:47Z")

</div>

I thought about it, but I also need a script to launch Julia in the first place, so I also need to rewrite this bash script:

```julia
#!/bin/bash
if [[$(basename $(pwd)) == "bin"]]; then
    cd ..
fi

julia_version=$(julia --version | awk '{print($3)}')
julia_major=${julia_version:0:3}

export EDITOR="gedit"

if test -f "bin/kps-image-${julia_major}.so"; then
    echo "Found system image!"
    julia -J bin/kps-image-${julia_major}.so -t 1 --project
else
    julia --project
fi

```

And if I would replace this script with a Julia script, I guess latency and memory consumption would be significantly higher.

Finally, you cannot fully replace bash or bat or whatever scripts to start Julia itself because if you want to start Julia with parameters you need a script. This would become unnecessary only if Julia

- would allow to use a config file to configure the launch options
- and Julia would be able to load a version specific system image automatically

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 25, 2022, 4:30pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/19 "2022-05-25T16:30:45Z")

</div>

Here the translation of the run\_julia script to .bat for Windows users:

```julia
@echo off
REM This script launches julia and loads a version specific
REM system image from the folder bin if it exists.
REM The name of the system image must be similar to
REM kps-image-1.7.dll
REM It launches julia with one thread.

REM determine basename of current directory
for /F "delims=" %%i in ("%cd%") do set basename=%%~ni

if %basename%==bin cd ..
for /f "delims=" %%i in ('julia --version') do set version_string=%%i
for /f "tokens=3 delims= " %%a in ("%version_string%") do set version=%%a

set julia_major=%version:~0,3%
set image=kps-image-%julia_major%.dll

set EDITOR="code"

IF EXIST "bin/%image%" (
    echo Found system image!
    julia -J bin/kps-image-%julia_major%.so -t 1 --project
) else julia --project

```

tested! 🙂

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 25, 2022, 4:35pm UTC](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652/20 "2022-05-25T16:35:49Z")

</div>

> [@ufechner7](#):
>
> I thought about it, but I also need a script to launch Julia in the first place

Why is that easier than just running a Julia script (which can launch additional Julia processes as needed, see below)?

> [@ufechner7](#):
>
> And if I would replace this script with a Julia script, I guess latency and memory consumption would be significantly higher.

Surely any differences here are negligible compared to the cost of creating a system image.

> [@ufechner7](#):
>
> Finally, you cannot fully replace bash or bat or whatever scripts to start Julia itself

Sure you can:

```julia
julia> run(`$(joinpath(Sys.BINDIR, Sys.iswindows() ? "julia.exe" : "julia")) --version`)
julia version 1.7.1
Process(`/Applications/Julia-1.7.app/Contents/Resources/julia/bin/julia --version`, ProcessExited(0))

```

You can literally do everything in Julia that you would do in a shell script (except more easily and portably), including launching additional `julia` processes, with the added advantage that Julia knows its own path.

[Next page](https://discourse.julialang.org/t/how-to-translate-bash-to-bat/81652.md?page=2)
