title: Changing user in bash script
body: <p>I wanted to create an installation script for my raspberry pi which secures the default installation by configuring/hardening ssh, installing a firewall and fail2ban and finally to get rid off the default user of Raspbian. All other parts work but the final part always shows me an error. 
  The new user is created and added to the sudo group. After that I want to delete the old user 'pi'. As the script runs with sudo in the user context of 'pi' I thought I could solve this by switching to 'su' but I just get an error that the user couldn't be deleted as it is used by a process: </p>
  
  <pre><code>echo "Enter the new user name? Only lower case letters allowed!" 
  read user
  sudo adduser $user &amp;&amp; adduser $user sudo
  echo "default user 'pi' will now be deleted"
  su -c "deluser -remove-home pi"
  </code></pre>
  
  <p>If I check with 'users' the user 'pi' is gone but I can still log in with this account. How can I solve this problem inside the script?</p>
  
  <p>I tried the answers I found here: <a href="https://stackoverflow.com/questions/1988249/how-do-i-use-su-to-execute-the-rest-of-the-bash-script-as-that-user">How do I use su to execute the rest of the bash script as that user?</a> and here: <a href="https://unix.stackexchange.com/questions/361327/how-to-login-as-different-user-inside-shell-script-and-execute-a-set-of-commands">https://unix.stackexchange.com/questions/361327/how-to-login-as-different-user-inside-shell-script-and-execute-a-set-of-commands</a> but nothing seem to work. I searched Google but I can't find any solution that works. Is it even possible what I'm trying to?</p>