Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do scripts define common commands in variables?

Tags:

bash

shell

sh

I see this all the time at my place of work:

#!/bin/sh

.....

CAT=/usr/bin/cat                                  # An alias for cat
MAIL=/usr/bin/mail                                # An alias for mail
WC=/usr/bin/wc                                    # An alias for word count
GREP=/usr/bin/grep                                # An alias for grep
DIRNAME=/usr/bin/dirname                          # An alias for dirname
RM=/usr/bin/rm                                    # An alias for rm
MV=/usr/bin/mv                                    # An alias for mv

.....

Is it just my company that does this? Is there a reason why you would want to spell out where these extremely common commands are? Why would I want $CAT to refer to /usr/bin/cat when cat already refers to /usr/bin/cat? Am I missing something? It seems like its needlessly redundant.

like image 919
R4F6 Avatar asked Oct 13 '15 19:10

R4F6


1 Answers

Using the full pathname ensures that the script operates correctly even if it's run by a user who customizes their PATH environment variable so that it finds different versions of these commands than the script expects.

Using variables simplifies writing the script, so you don't have to write the full pathname of a command each time it appears in the script.

like image 163
Barmar Avatar answered Oct 05 '22 05:10

Barmar