32

should I use the text editor? the extension should be .sh or .bat?

Sange
  • 321
  • 1
  • 3
  • 3
  • 1
    Your question is more likely to receive useful answers if it contains some specifics. Such as what type of data is to be processed, including realistic examples of input and output. – J. Starnes Nov 03 '17 at 00:54
  • @SergiyKolodyazhnyy The duplicate nominee is closed as too broad. As such the close vote on this one should be "too broad" as well. That said it seems like a reasonable question to me as I was once in the dark myself :) – WinEunuuchs2Unix Nov 03 '17 at 01:13
  • @WinEunuuchs2Unix It is closed but does have an accepted answer, which will give OP what they want, or at least a starting point. – Sergiy Kolodyazhnyy Nov 03 '17 at 01:30
  • @EliahKagan If a mod agrees perhaps he can reopen that one and then merge the answers from here to there? – WinEunuuchs2Unix Nov 03 '17 at 10:56
  • @WinEunuuchs2Unix That question, which this received close votes to be duped to, was actually reopened a few hours ago, then closed again as duplicate of that highly voted question. Both of them are listed as "originals" in the duplicate banner here (i.e., this question is duped to both of them). So if answers are to be merged, they'd probably be put there. Do you think that would be useful? – Eliah Kagan Nov 03 '17 at 11:04
  • Yes Seth's question in your last link has the most upvotes and contains references. Everything else could loosely be defined as a duplicate. The uniqueness of this question is the correlation to .bat files which are extension sensitive including .com and .exe whereas the "Linux-way" as best I understand is to read the first bytes of a file to determine it's OOP (Object Orientated Programming), ie #!/bin/bash. – WinEunuuchs2Unix Nov 03 '17 at 11:48

3 Answers3

40

To create one use the .sh extension but it doesn't really matter but it helps future users to quickly determine which file type it is. The bat name is mostly used on Windows but in Linux file name extensions do not really matter. Meaning I can call my file say run.de and it would still run in bash file but I believe it's a good practice to name them with the .sh file extension.

For the editor, part uses any that is best for you between nano vim gedit emacs, but I believe gedit would be nice to start with.

How to:

using gedit text editor:

  1. Create the file:

    gedit runme.sh
    
  2. Add code into file:

    #!/bin/bash 
    
    
    echo "Hello World!"
    
  3. Make file executable:

    chmod +x runme.sh
    
  4. Run the file from terminal:

    ./runme.sh
    
George Udosen
  • 36,677
5

In linux they are bash scripts. You can use most any editor and you can call it what you wish, Linux does not use extensions such as .sh or .bat or .exe to .doc to identify files, it uses magic.

Probably gedit for a graphical editor and nano from the command line.

Avoid word processors such as libre office as they add headers you don't want.

http://www.linfo.org/magic_number.html

To write a bash script , start the file with a "SheBang" or #!/bin/bash

#!/bin/bash

# comments start with a '#"

command 1
command 2

Tons of tutorials on the web - https://linuxconfig.org/bash-scripting-tutorial

Gunnar Hjalmarsson
  • 33,540
  • 3
  • 64
  • 94
Panther
  • 102,067
2

Batch file equivalent in linux is shell script (.sh). You can use gedit, vim or any other text editor available to create one.

A good start for begginers is http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html.

Hope this helps :)

Videonauth
  • 33,355
  • 17
  • 105
  • 120
Alex Y
  • 21
  • 1