Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

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 is install 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:

install -b -m 755 /dev/stdin ~/testbin << EOF
#!/bin/sh
echo "Success!"
EOF 

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 use install'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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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