8

My code to create an executable script

touch script.sh && echo "echo 'Hello World'" > script.sh && sudo chmod +x script.sh
2
  • 2
    @ShamFiorin I wanted to emphasize the first part of desserts answer. You should try to use sudo very sparingly. Getting into the habit of throwing it around can be dangerous. In this case you own the script you created so you can do what ever you please with the permissions, no sudo needed.
    – Marie
    Jul 30, 2018 at 17:19
  • 2
    Related in a general way: Tips for golfing in Bash Jul 30, 2018 at 21:01

4 Answers 4

20

The install command (part of GNU coreutils along with cp, mv, rm, etc.) can copy a file while also setting its ownership and permissions and creating parent directories as necessary. By default it will make the new file executable. It doesn’t understand ‘-’ but /dev/stdin can be used instead:

install /dev/stdin script.sh <<< "echo Hello World"

The above uses a ‘here string’ to provide the text on stdin, which is slightly shorter than piping it in. The arguments to echo don’t need quoting unless they contain characters with special meaning.

2
  • note that herestring is a Bashism - a heredoc would be portable
    – Zombo
    Jul 30, 2018 at 15:12
  • 3
    Nice idea. You could also use a process substitution: install <(echo echo Hello World) script.sh Jul 30, 2018 at 21:00
18

You can leave out the whole touch command and there’s no need to use sudo. To shorten the command line further you can use bash History Expansion to save the filename the second time, and omit unnecessary spaces:

echo "echo 'Hello World'" >script.sh&&chmod +x !#:3

History Expansion replaces !#:3 with the fourth word from the current command line, which in your case is the filename script.sh. Of course this needs to be adapted in case you modify the first command, e.g.:

>script.sh echo "echo 'Hello World'"&&chmod +x !#:1

Note that a valid script needs a shebang:

>script.sh echo -e "#!/bin/bash\necho 'Hello World'"&&chmod +x !#:1

If you need this chain of commands more often I recommend defining a function, this way you’d just need to type the function name. A similarly adaptable – though longer – way is the use of a variable holding the filename:

f=script.sh;>$f echo "echo 'Hello World'"&&chmod +x $f

If your filename contains special characters don’t forget to quote them properly.

0
8

I prefer to use cat for tasks like this:

cat > script.sh && chmod +x script.sh
#!/bin/sh
echo 'Hello World'Enter
Ctrl+D
0
1

You could also leave out the whole chmod and use umask instead before the echo. Should save you a few bytes. Shebang is optional as long as you use commands that are understood by every shell (bourne and C mainly).

2
  • 5
    How would you do that exactly?
    – wjandrea
    Jul 30, 2018 at 21:52
  • It's not possible to use umask for setting executable bit by default...
    – Ravexina
    Aug 11, 2018 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .