0

For example, if I want to rename the file old_name new_name, I can use the following code:

args='old_name new_name'
mv $args

However, if the original filename is old name, neither of the following code is able to work as intended:

args='"old name" new_name'
mv $args
# mv: target 'new_name' is not a directory
args='old\ name new_name'
mv $args
# mv: target 'new_name' is not a directory

new_name is recognized as the third arguments. It seems that characters like " and \ lose their special meanings after expanded from a string.

How to properly pass a string with blanks as separate command line arguments? In this case, is there a way to specify that old and name should be grouped together?

Thanks!

chrt
  • 21
  • 5
  • Relevant: [BashFAQ/050](https://mywiki.wooledge.org/BashFAQ/050) – Benjamin W. May 29 '19 at 19:43
  • Summary: you should use an array, as in `args=('old name' new_name)`, and then `mv "${args[@]}"`. There are many similar questions. – Benjamin W. May 29 '19 at 19:44
  • Like this one: https://stackoverflow.com/questions/36129045/pass-parameters-that-contain-whitespaces-via-shell-variable – Benjamin W. May 29 '19 at 19:46
  • Thank you @BenjaminW. ! I wish I had found this, but I didn't come up with a good way to describe my question ... – chrt May 29 '19 at 20:00

1 Answers1

0

Arrays were added to the language precisely to address this use case (not as a general-purpose container):

args=("old name" "new name")
mv "${args[@]}"
chepner
  • 497,756
  • 71
  • 530
  • 681