When writing answers on here, it's often necessary to create a new script and chmod +x
it to make it executable. I won't go into whether this is a good thing or not, but explaining to people that they need to fire in three separate command (and teaching them to use nano
!) is a bit of a waffle.
Is there anything easier?
Best Answer
Why yes there is! Let me introduce you to the
install
command.install
is a GNU-standard application that lets you copy files around and specify certain things at the same time. The short syntax isinstall SOURCE DESTINATION
.However, "certain things" are important. You can specify the file mode with
-m xxx
(755 for writeable by owner and executable by anyone). And bash has a couple of tricks for redirecting new files and here-doc for accepting long-form data. Here's a quick example:Now you can run
~/testbin
and it should echo out.You can combine this with
sudo
for root-owned files or if you want to write as somebody else, you can sudo and useinstall
's-o
owner flag.The
-b
flag just creates a backup if you run the command a second time. This is helpful if you're doing potentially destructive tasks.