Hi. I’m no coder, and new to Bash. I’m trying to write a Bash script that installs the server files for a game called Insurgency Sandstorm. The script works if I set my username and home directory explicitly in a certain part of the script, but I want to use variables in place of my explicit username and home directory.
sudo bash -c 'cat <<EOF > /etc/systemd/system/1insserver.service
Description=Sandstorm Server 1
My username and home directory are on the last lines beginning with User= and ExecStart=. If I use $USER and $HOME in place of them, it uses root’s name and home folder (due to the use of sudo I assume). If I declare different variables at the top of the script (such as $USR and $Homedir), the entries when I run the game server are blank:
How can I set $USER and $HOME inside that ‘sudo bash -c’ part? Thanks.
First of all, why create a bash script from the command line and risk making typos that you cannot correct? Use a text editor — there are plenty of them available, both GUI-based ones and console-based ones. GUI editors like
kate et al are commonly also
polkit-aware and will ask you for a password when writing to any area outside your home directory.
If you want to literally quote variable names on the command line, then you must quote them with single quotes in order to not have the shell expand them.
'$HOME', but better is to single-quote the entire path, like so:
Neither of those two variables exist. The variable for the home directory is
$HOME, and for the username,
Note 1: If you edit and save the file in a text editor, then you should use double quotes instead.
Note 2: Given that you’re writing a
systemd service, you should launch this service as a regular user.
systemctl enable --user --now 1insserver
Got it working thanks to your help and tips. Much appreciated!
This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.