I need a script to create a screen session and run a command inside, then detach and proceed with other screens. So I need something like screen -S vault -d -m $HOME/vault/v.sh. However, v.sh requires sudo privileges. What are my options?

  1. Leave it like that and run the whole script as sudo. Problem: the other scripts/sessions will also run as sudo, which is undesirable.
  2. Insert sudo at the start: sudo screen -S vault -d -m $HOME/vault/v.sh.
  3. Insert it before calling v.sh: screen -S vault -d -m "sudo $HOME/vault/v.sh"

What is the best option?

  • 1
    It depends on what you want to achieve. It's someone you have to make your mind up about. it looks like you know how to solve your problem. You even list some of the pro and con arguments.
    – Seth
    Jan 17, 2019 at 10:49
  • Well, I'm still a noob here, being a long time Windows user. So I definitely don't know what I'm doing.
    – ulu
    Jan 24, 2019 at 9:40

1 Answer 1


One option you haven't listed is that if v.sh itself requires sudo privs, put that in the shebang line at the top of the script:

$ head -1 $HOME/vault/v.sh
#!/usr/bin/sudo /bin/sh

This method has the slight advantage that your master script doesn't require any knowledge of which scripts require sudo and which don't.

  • The problem with putting sudo in v.sh is that it requires entering the password inside the screen. SO I have to manually go to the screen and enter it, which I'm trying to avoid.
    – ulu
    Jan 24, 2019 at 9:43
  • One way to get around that is to edit your sudoers file to allow your account to run the script without entering a password. A line in your sudoers file like ulu ALL=(root) NOPASSWD: /bin/bash /home/ulu/vault/v.sh might be what you need.
    – Jim L.
    Jan 25, 2019 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.