title: How to change between speakers and USB headset using a script on Ubuntu?
body: <p>I am trying to write a script to change between speakers and headset. I want to be able to change the output and the input with a shortcut.</p>
  
  <p>I used these commands to see my audio devices :</p>
  
  <pre><code>$ pacmd list-sinks | grep alsa_output 
  
  name: &lt;alsa_output.usb-Logitech_Logitech_USB_Headset-00.analog-stereo&gt;
  name: &lt;alsa_output.pci-0000_00_1b.0.analog-stereo&gt;
  
  $ pacmd list-sources | grep alsa_input
  
  name: &lt;alsa_input.usb-Logitech_Logitech_USB_Headset-00.analog-mono&gt;
  name: &lt;alsa_input.usb-093a_262c-01.analog-mono&gt;
  name: &lt;alsa_input.pci-0000_00_1b.0.analog-stereo&gt;
  </code></pre>
  
  <p>And wrote this script so I can change the default audio device.</p>
  
  <pre><code>#! /bin/bash
  
    pacmd set-default-sink alsa_output.usb-Logitech_Logitech_USB_Headset-00.analog-stereo
    pacmd set-default-source alsa_input.usb-Logitech_Logitech_USB_Headset-00.analog-mono
  
  exit 0
  </code></pre>
  
  <p>When I run this with the command </p>
  
  <pre><code> sudo ./usb-speakers.sh 
  </code></pre>
  
  <p>I get this messages :</p>
  
  <blockquote>
    <p>No PulseAudio daemon running, or not running as session daemon.<br>
    Home directory not accessible: Permission denied<br>
    No PulseAudio daemon running, or not running as session daemon.  </p>
  </blockquote>
  
  <p>I found several questions with similar problems but I can't make anything work for me.</p>