Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
    
Don't hate, just guide my poor soul. – OskyEdz Snakehult Dec 15 '15 at 23:09
    
@ekaj Yes, the directory /home/bukkit/Server exists. – OskyEdz Snakehult Dec 15 '15 at 23:19
    
@ekaj hmm, I'll test it. – OskyEdz Snakehult Dec 15 '15 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. – steeldriver Dec 15 '15 at 23:28
1  
That did it, thanks man! – OskyEdz Snakehult Dec 15 '15 at 23:34
up vote 1 down vote accepted

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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