How to set `DEBIAN_FRONTEND=noninteractive` from powershell?
Scenario
While implementing an unattended installation of some software on WSL Ubuntu 18.04 from Windows Powershell, the apt upgrade comand returns the following prompt:
This question nicely explains that one can prevent this prompt by setting the environment variable in WSL Ubuntu 18.04 before running the apt upgrade command. I verified this indeed works. However I am experiencing some issues setting that environment variable for WSL Ubuntu 18.04 from Powershell. These issues are elaborated on in section "Attempts".
Question
How can I set the environment variable DEBIAN_FRONTEND=noninteractive in WSL Ubuntu 18.04 from a powershell script?
MWE
An MWE with some attempts is included below.
# First define path to the installed ubuntu1804.exe
$str1="/Users/"
$str2="/AppData/Local/Microsoft/WindowsApps/ubuntu1804"
$hdd_name=(Get-WmiObject Win32_OperatingSystem).SystemDrive
$username=$env:UserName
[String] $ubuntu1804_path=$hdd_name+$str1+$username+$str2
Write-Host "Done with setup now starting apt update ."
# perfrom apt update
$str1=" run 'yes | apt update'"
$command=$ubuntu1804_path+$str1
invoke-expression -Command $command
Write-Host "Performed Update, now setting env variable."
# set environment variable DEBIAN_FRONTEND=noninteractive
$str1=" run DEBIAN_FRONTEND=noninteractive"
$command=$ubuntu1804_path+$str1
invoke-expression -Command $command
Write-Host "Performed setting env variable, now performing upgrade."
# perfrom apt upgrade
$str1=" run 'yes | apt upgrade'"
$command=$ubuntu1804_path+$str1
invoke-expression -Command $command
Write-Host "Performed upgrade."Attempts
Initially I tried to set the environment variable by parsing bash -c DEBIAN_FRONTEND=noninteractive from WSL. Next I tried wsl DEBIAN_FRONTEND=noninteractive. Then I tried the invoke-expression -Command as described in the MWE. All of these either hang on the prompt (the first 2 don't show the prompt but keep waiting indefinitely, (I waited 2hrs+ each, and verified the command in wsl itself takes only +- 30 mins)), the latter goes to the prompt in powershell:.
Doubts
I think the observations from the 3 attempts imply either of 2 cases:
- I am not setting the environment variable
DEBIAN_FRONTEND=noninteractivecorrectly from powershell. - Or, the powershell sets the environment variable
DEBIAN_FRONTEND=noninteractivecorrectly in some shell, but then opens a new shell for the next command, in which the environment variable is not set.
To verify I set the environment variable correct, I tried running wsl printenv, however this returns a different list then when I run the printenv from wls. If I manually set the environment variable DEBIAN_FRONTEND=noninteractive in wsl, the printenv command correctly displays the environment variable. However, if I close and re-open the WSL Ubuntu 18.04 and re-run the printenv command, the environment variable has disappeard. This observation implies option 2 of the doubts is possible. I have not yet found a way to determine whether the environment variable is set correclty from powershell, as it is not among the output of printenv in powershell, when it is in the WSL output of printenv.
1 Answer
After searching git for powershell scripts that contained DEBIAN_FRONTEND=noninteractive I found an example that shows one can specify an environment variable for a specific command, from a powershell script, with the following command:
# First define path to the installed ubuntu1804.exe
$str1="/Users/"
$str2="/AppData/Local/Microsoft/WindowsApps/ubuntu1804"
$hdd_name=(Get-WmiObject Win32_OperatingSystem).SystemDrive
$username=$env:UserName
[String] $ubuntu1804_path=$hdd_name+$str1+$username+$str2
# Run a command with an environment variable
$str1=" run DEBIAN_FRONTEND=noninteractive apt-get upgrade -y"
$command=$ubuntu1804_path+$str1
invoke-expression -Command $commandI verified this indeed prevents the prompt for any user interaction, which solves the XY-problem. Even though I am not yet able to verify this directly by inspecting the environment variables of that shell, I assume this indeed (temporarily) sets the environment variable DEBIAN_FRONTEND=noninteractive.