Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

The following is a simple script, named myscript, that I wrote in order to run a command.

dcmpath='$HOME/Data'
dcmfile='IM1'
dcm2nii $dcmpath/$dcmfile

Unfortunately, bash does not recognize the path in front of dcm2nii and I cannot run the last command in the file. I would be thankful if someone could let me know what I am missing. What would be the best way of customizing the path and file name which is the argument of a command?

share|improve this question
    
"bash does not recognize the path in front of dcm2nii" - What path? dcm2nii is the first thing on the line, there's nothing in front of it. –  Patrick May 19 '14 at 3:27
    
I meant dcm2nii does not find the file with the full path: $dcmpath/$dcmfile although the file really exist. –  A2009 May 19 '14 at 3:41

1 Answer 1

up vote 3 down vote accepted
dcmpath="$HOME/Data"
dcmfile="IM1"
dcm2nii "$dcmpath/$dcmfile"

In the first line, you should use double quotes instead of single quotes. Single quotes prevent variables like $HOME from being expanded; they're interpreted literally. You can see the bash manual for details: http://www.gnu.org/software/bash/manual/bashref.html#Quoting

It doesn't matter in the second line; both single or double quotes will work fine.

It's also good idea to (double) quote the argument in the last line, in case the variables contain a space.

share|improve this answer
    
Thank you. It worked perfectly. What is the difference between defining the variable surrounded by single quotation and double quotation? –  A2009 May 19 '14 at 3:27
    
@A2009 lk has explained that in the edit. –  Hauke Laging May 19 '14 at 5:31

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.