1

I am trying to write a bash script that creates folders and installs a software. Super user privileges are required to install the software, but it is not possible to create folders as a super user.

Here is an example of the script:

#!/bin/bash/
cd ~
mkdir Documents
pacman -S firefox

Does anyone have a clue how to overcome this conflict?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
BinaryBoy
  • 21
  • 3
  • 1
    You can definitely make folders as root, but if you want only parts of your script to be run as root, just put `sudo` in front of those commands. – jeremysprofile Aug 02 '18 at 16:44

1 Answers1

1

Instead of running your entire script as root, you could use sudo in your script to run only the pacman command as root:

#!/bin/bash
mkdir ~/Documents
sudo pacman -S firefox

This way the Documents/ folder will be created in your user's home directory and then you will be prompted for the root password in order to execute the pacman command as super user.


Note:

As @jeremysprofile stated in the comments:

You can definitely make folders as root

I suppose the problem is that you expect the Documents/ folder to be created inside your user's home directory. However, if you run you script as root, ~ expands to the super user's home directory: /root/. So this is where you will your Documents/ folder created with your current script.

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56