1

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!

8
  • Don't hate, just guide my poor soul.
    – OskyEdz
    Commented Dec 15, 2015 at 23:09
  • @ekaj Yes, the directory /home/bukkit/Server exists.
    – OskyEdz
    Commented Dec 15, 2015 at 23:19
  • @ekaj hmm, I'll test it.
    – OskyEdz
    Commented Dec 15, 2015 at 23:22
  • 4
    Variables don't get expanded inside single quotes, hence the system is looking for literal /home/$USERNAME/Server instead of /home/bukkit/Server. Use double quotes instead. Commented Dec 15, 2015 at 23:28
  • You can also just not use quotes and do MCPATH=/home/$USERNAME/Server
    – cutrightjm
    Commented Dec 15, 2015 at 23:40

1 Answer 1

3

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?

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.