0

I'm not entirely sure where to ask or how to formulate this question with correct terminology.

I have script here for managing a game (minecraft) server for the user (bukkit). I've cut out some variables and I want to know why this is not working and a possible solution.

#!/bin/bash
# /etc/init.d/bukkit

USERNAME='bukkit'
MCROOT='/home/$USERNAME' 
MCPATH='/home/$USERNAME/Server'
BACKUPPATH='/home/$USERNAME/backups'
SCRIPTLOG='/home/$USERNAME/script.log'
LOGPATH='/home/$USERNAME/Server/logs'

(line 50) cd $MCPATH

(I also like to have $MCROOT in the paths followed by it.)

Running the script gives:

/bin/bukkit: line 50: cd: /home/$USERNAME/Server: No such file or directory
-su: line 0: cd: /home//Server: No such file or directory

The point is to have different USERNAME in different files and I thought I'd save time editing all paths be replacing everything with one variable normally in the rest of the code. So my other file called "ftb" has USERNAME='ftb'

"/bin/bukkit" is a symbolic link to "/etc/init.d/bukkit"

Have a nice day!

1 Answers1

2

Variables don't get expanded inside single quotes, hence the system is looking for literal /home/$USERNAME/Server instead of /home/bukkit/Server.

Your code should work if you use double quotes instead e.g.

MCPATH="/home/$USERNAME/Server"

There is a more detailed discussion at What is the significance of single and double quotes in environment variables?

steeldriver
  • 81,074