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

This question already has an answer here:

I want to rename a file dynamically based on data which contains spaces

Name="Abc DEF"
Name1="GHI JKL"

mv /sample/pdf/noriginalName.pdf /sample/outputPdf/${NAME}${Name1}".pdf"

But it gives me an error because it encounters space in between.

How can I do this?

share|improve this question

marked as duplicate by Gilles, polym, slm Aug 14 '14 at 23:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Is it Name or NAME? – Cristian Ciupitu Aug 14 '14 at 12:30

You must quote the whole file name:

mv /sample/pdf/noriginalName.pdf "/sample/outputPdf/${NAME}${Name1}.pdf"

Using double quote prevent bash and other Bourne-like shells (except zsh) perform Field splitting after your variable is expanded.

share|improve this answer

Don't worry, you're not the first and won't be the last one to be bitten by that. It's the most frequently asked question about shells, and that's responsible for most bugs and security vulnerabilities found in shell scripts. That's due to a bad design choice of the Bourne shell and the Korn shell in the earlier Unix history.

In the Bourne shell and most of its descendants (ksh, ash, pdksh, mksh, bash, dash, yash...), leaving a variable unquoted is the split+glob operator.

Best demonstrated with:

$ var='*.txt,.*'
$ IFS=',' # default is space tab and newline
$ printf '<%s>\n' $var

The shell has split $var on , characters and expanded each of the resulting words (which in this case contain wildcard characters) in the list of files matching the corresponding pattern.

That's why, in Bourne-like shells (except zsh), you must always quote your variables (unless you do want the split and/or glob)

$ printf '<%s>\n' "$var"

So in your case:

mv /sample/pdf/noriginalName.pdf "/sample/outputPdf/${NAME}${Name1}.pdf"


mv /sample/pdf/noriginalName.pdf /sample/outputPdf/"${NAME}${Name1}".pdf

(where you put the quotes doesn't matter as long as the variables are quoted).

You can disable the splitting only with:


And you can disable the globbing only with:

set -f # affects all globbing, not only the one done upon variable expansion.


IFS=; set -f

You get the same behaviour as zsh for parameter expansion, no globbing, no splitting. You still need to quote your variable however in

cmd "$var"

If you want cmd to be passed one empty argument when $var is empty or unset (as opposed to no argument at all). A typical example is [ -n "$var" ] where if you don't quote $var, the test will alway be true since when $var is empty, that resolves to [ -n ] instead of [ -n '' ].

In zsh, globbing and splitting is not done implicitly upon variable expansion (except in sh or ksh emulation mode). You have to request it explicitly using specific expansion operators: $=var for word splitting, $~var for globbing (or ${~var}; $~=var for both).

The other shells that don't have this kind of problem are fish, and rc and its descendants (es, akanga).

In (t)csh:

cmd "$var"

helps but is not enough, as it doesn't work if $var contains newline characters. There, you need:

cmd $var:q
share|improve this answer

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