-1

In a script, I got the following code from a ksh script :

 for log_file in `cat a_filename`
 do
    `echo mv from_directory/$log_file to_directory`
 done 

Could the code be

 for log_file in `cat a_filename`
 do
    mv "from_directory/$log_file" to_directory
 done 

instead ?

What is the plus-value of using echo with backtick ?

Michael Homer
  • 76,565
Luc M
  • 4,095
  • 5
  • 30
  • 29

1 Answers1

3

Absolutely none. There is no reason whatsoever to use backticks in that place of the code. And backticks in general should be replaced by $(...) which nests nicer and that handles quoting intuitively.

Seriously. Don't write code like that.


I can see where it may have come from though:

  1. Someone wrote a quick and dirty script to move some files somewhere.
  2. They inserted an echo to "make it safe". This made it possible to see what commands would have been executed, without the risk of overwriting anything, while testing. (Good thing)
  3. Someone else decided that they needed to get those commands executed, so they just put everything in backticks.
  4. Profit!
  5. Shame!

Related question: When should one use $( ) in defining variables

And: What's the difference between $(stuff) and `stuff`?

Kusalananda
  • 333,661
  • 2
    User stated it is a ksh script. Some older versions of ksh do not support the $( ..,) syntax. – fpmurphy Sep 06 '17 at 03:54
  • 1
    @fpmurphy1 My apologies, but I don't see how that matters. If the OP is in the process of revisiting old scripts, they may as well update them properly. Also, if they are stuck with such an old shell (which must be pre-ksh88), then I'd be extremely worried about what else on the system has not been updated. – Kusalananda Sep 06 '17 at 06:01