12

I'm trying to write a script to run one of my .jar files as daemons, but I am not understanding how to create a .sh extension file in Ubuntu. I have already used vi to create a file with the code that I want, but I cannot figure out how to define the file to specifically be a .sh file. For example, I want to convert my file "foo" or "foo.txt" into "foo.sh".

Is there a simple command I am not seeing that can convert files to a .sh extension or is it a more complicated process?

peterh
  • 11,875
  • 18
  • 85
  • 108
vikas
  • 471
  • 3
  • 15
  • 31
  • 1
    `touch foo.sh`, `vi foo.sh`, `cat /dev/urandom > foo.sh`??? – devnull Oct 10 '13 at 07:02
  • 1
    you should always do through research before asking here. There are so many tutorials explaining it http://linuxcommand.org/writing_shell_scripts.php – DevC Oct 10 '13 at 07:02
  • DevC, I used `CTR-f` on every page of those tutorials (both deprecated and current), and it does not mention ".sh" ever. I've also visited 3 other sites attempting to find an answer to this question and not a single one even mentions ".sh". Did that link previously lead to instructions that don't exist anymore? – Sean Branchaw Feb 11 '16 at 16:19

1 Answers1

25

Use this as a template:

#!/bin/bash

# content of your script

The first line is called a shebang and tells the OS what program that should be used executing the content of the file, in this case, bash.

To make the file executable:

chmod 755 foo.sh

To execute your script do:

./foo.sh
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • If you run into problems that the command or file is not found, see here: https://stackoverflow.com/q/7362504/1066234 - And for my system, to execute the script I used `bash foo.sh` – Avatar Mar 24 '20 at 15:48