Disable output of systemctl and apt-get

I'm currently writing a script that installs and configures the Samba server automatically for me. I was wondering why this command sudo apt-get -y install samba > /dev/null && sudo systemctl enable smbd.service > /dev/null still gives this output.

Extracting templates from packages: 100%
Synchronizing state of smbd.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable smbd

How can I prevent the commands from giving any output?

Thanks

Ubuntu Server 20.04.3 LTS


Solution 1:

> will redirect only stdout. stderr stream should be redirected too. Same commands as below:

sudo apt-get -qq install samba 1> /dev/null 2> /dev/null && sudo systemctl enable smbd.service 1> /dev/null 2> /dev/null

  • apt-get..-qq will suppress more installation dialogs
  • 1> /dev/null discards STDOUT
  • 2> /dev/null discards STDERR

Solution 2:

If your script uses bash, then you should use the &> operator for redirecting any output, that is both stderr and stdout:

sudo apt-get -y install samba &>/dev/null \
&& sudo systemctl enable smbd.service &> /dev/null