How to declare a string array in Apptainer `.def`? The following fails.

`debian.def`:
```
Bootstrap: docker
From: debian:latest

%post
apt update
packages=(
htop
# Test
vim
)
apt install -y "${packages[@]}"
```

```bash
apptainer build my_debian.sif debian.def
```
```
WARNING: 'nodev' mount option set on /tmp, it could be a source of failure during build process
INFO: Starting build...
INFO: Fetching OCI image...
47.0MiB / 47.0MiB [===============================================================================================================] 100 %0s
INFO: Extracting OCI image...
INFO: Inserting Apptainer configuration...
INFO: Running post scriptlet
+ apt update
Get:1 http://deb.debian.org/debian trixie InRelease [140 kB]
Get:2 http://deb.debian.org/debian trixie-updates InRelease [47.3 kB]
Get:3 http://deb.debian.org/debian-security trixie-security InRelease [43.4 kB]
Get:4 http://deb.debian.org/debian trixie/main amd64 Packages [9671 kB]
Get:5 http://deb.debian.org/debian trixie-updates/main amd64 Packages [5412 B]
Get:6 http://deb.debian.org/debian-security trixie-security/main amd64 Packages [165 kB]
Fetched 10.1 MB in 1s (12.3 MB/s)
All packages are up to date.
/.post.script: 2: Syntax error: "(" unexpected
FATAL: While performing build: while running engine: while running %post section: exit status 2
```
May 20, 2:50pm

The %post section in Apptainer .def files runs as /bin/sh by default, which does not support Bash arrays ((...) and ${array[@]}).

Fixes

Option 1: Use Bash explicitly

Add a shebang as the first line of %post:

diff
%post
+ #!/bin/bash
    apt update
    packages=(
        htop
        vim
    )
    apt install -y "${packages[@]}"

Option 2: POSIX-compatible (no arrays)

diff
%post
    apt update
-   packages=(
-       htop
-       vim
-   )
-   apt install -y "${packages[@]}"
+   apt install -y htop vim

or with a space-separated string:

diff
%post
    apt update
+   packages="htop vim"
+   apt install -y $packages

Use Option 1 if you need arrays; otherwise, Option 2 is simpler and more portable.

May 20, 2:50pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Le Chat can make mistakes. Check answers. Learn more