The equivalent to a DOS batch file would be a shellscript.
To create a shellscript, you can just use any text editor.
Whichever editor you use is completely up to you.
You could use editors like nano, vim, neovim, emacs, spacemacs, joe etc. in the terminal, or you could use a GUI based text editor - Kate, leafpad, gedit, gvim, Caja, the GUI version of emacs/spacemacs, neovim etc etc..
Whatever Linux distro you have installed, you will almost certainly have nano and vi pre-installed in the terminal. vi is just vim-minimal with the 'compatible' option turned on, which makes vim behave exactly the same as the classic vi. vi/vim aren't for everybody though. Personally, I love it.
But nano is probably the simplest terminal based text editor. So you might want to try nano.
Alternatively, your desktop environment should also have at least one GUI based text editor pre-installed. But exactly which one you have will depend entirely on the Desktop you have installed.
You could give your shellscripts a .sh extension, in order to identify them as shellscripts, but that is entirely optional. In Linux, we generally use a shebang line as the first line of our scripts, which tells the shell which interpreter to use to run a script.
So for a generic shellscript, we'd start our shellscript with a shebang like:
or
That tells the shell to use whatever the default minimal shell is. Very often sh just redirects to bash nowadays, but in some distros, sh redirects to a more minimalist shell like dash, or ash.
But when writing a plain shellscript - you have to be careful to avoid any shell-specific extensions to the shellscript language - you have to write your shellscript as generically as possible. That will allow you to write generic shellscripts that can be sent to different systems and can run on any compatible shell. And on Linux there are loads of different shells available.
Alternatively, you could target a particular shell/interpreter.
Most Linux distributions use Bash as the default shell. A few use other shells as the default, but bash is usually installed alongside the default shell too.
So I'd recommend learning and targetting bash. Bash has added some really useful extensions to their inplementation of shellscript. ksh and zsh are two other popular shells - there are a few others.
Each of those support generic shellscripting, but also have their own additional extensions and features.
But bash is probably your best bet when starting out. It's the most commonly used and it's just everywhere!
In order to write a shellscript specifically for bash, you'd use one of the following shebang lines:
Or
If you were writing a script for zsh, or ksh, or some other shell - then you'd swap out bash in the above for the path to whatever shell you want to use to run your script.
IMPORTANT:
The shebang line MUST be the very first line in your script. Without a shebang, the interpreter won't know which program to run in order to run/interpret the script.
Let's say we open a text editor and create a "Hello world!" script
Bash:
#!/usr/bin/env bash
echo "Hello world!"
Open up a text editor - either in a terminal, or in the GUI and enter the above two lines of code.
After saving the scirpt as "Hello" at the root of our home directory, we open a terminal to our home directory (if we aren't already in a terminal) and give the script executable permissions:
Now we have an executable shellscript in our home directory called Hello.
To run it, we would typically use:
Because we have the shebang line - the shell will know to run our script using bash.
When running one of the scripts in our home directory, we need to prefix our script name with
./
because when we enter any command our shell (bash, or zsh, etc) looks in directories specified in $PATH.
Our home directories are NOT listed in $PATH.
So if we tried running our script using:
We'd get an error saying something like:
This is because our script was not found in any of the directories specified in the shells $PATH environment variable.
So in order to run our script, we need to specify the path to our script.
So, assuming we're in the terminal and we're in the root of our home directory (which is where we saved the Hello script), we need to specify the current directory as the path to the script.
So we run our script using:
Which tells the shell to run the file called Hello in the current directory.
If we decide to move into our Downloads directory:
So now we're at /home/username/Downloads and our script is at /home/username/Hello.
If we want to run our script from the current location, we would either have to use:
Where
../
means "the previous directory" in the file-system hierarchy.
This is the relative path to our script - relative to the current directory.
Alternatively, we could run it using:
Where username is your actual username.
This is the absolute path to our script, because it specifies the path to our script from the root
/
of the file-system.
Or:
Where $HOME is the environment variable that holds the absolute path to our home directory (/home/username/).
Or:
Where
~/
is a shortcut for /home/yourusername, or $HOME.
Who knew there were so many ways to run a script?!
With your scripts, you can go one step further and put them somewhere in $PATH.
So if you wrote a script that is REALLY useful and you want it to be available system wide, you could consider copying it as root to /usr/local/bin/. Then you'd be able to run your script just by invoking its name in the terminal without having to specify a path to it.
Or if your script is something that you want to keep to yourself and you want to just be able to run your scripts from anywhere on your system without having to specify the full path to it each time - you could set up a personal bin directory in your /home directory. I won't go into any detail on that here, that's well beyond the scope of this particular reply. But I have posted instructions on how to set up a personal bin directory in other posts here. I highly recommend it. Once you've set up a personal bin directory, you can copy any scripts or executables that you've built into it and can run them as if they are any other terminal command.
Additional notes on the shebang line:
If we didn't have a shebang line, the shell would not know which interpreter to use and would just assume that the script contained commands for the running shell - which will be bash a lot of the time, so no harm there.
However, you're not just limited to shellscripts. You can use a shebang line as the first line in ANY script, in ANY scripting language you like.
So for Python3, you'd specify the path to python3 in the shebang.
For Perl, or Lua, PHP or even more obscure scripting languages like lolcat - you can use a shebang to specify the path to the correct interpreter for those languages too.
Bearing this in mind - if our "Hello" script was a Python3 script and we didn't use a shebang to specify that python3 should be used, the shell would attempt to run the python3 script as a series of shell commands and you'd potentially end up getting a bunch of errors output to the screen.
So again - that is why we always include a shebang line as the first line of ANY script.
The main advantage of using a shebang is that you don't need to use a file-extension at the end of your file-name to differentiate the type of script. That way you can make your scripts look like any other installed command.
So instead of having to call our script Hello.sh - we can simply call the script Hello.
Running
./Hello
just feels a lot nicer than having to type
./Hello.sh
, it just makes your scripts seem more like any other installed command.
If you take a look at the files in your /usr/bin/ directory - you'd be surprised at the different types of files in there. Many will no doubt be compiled, executable ELF binaries built using C/C++, Rust, or some other compiled programming language. Some will be symbolic links to other files (symbolic links are like shortcuts in Windows). But a good number of them will be scripts - shellscripts, Python scripts, Perl scripts, Lua scripts, ruby scripts etc etc.
Almost none of the script-based commands in /usr/bin/ will have any identifying file extensions like .py, or .pl, etc. And this is purely because we use shebangs.
Without a shebang line, the only other way to run a script would be to run the interpreter in the shell and pass it the name of the script.
e.g.
Bash:
bash /path/to/MyShellscript
or
Bash:
python3 /path/to/MyPython3Script
Again - not as convenient as having a shebang in the script and having our script in a personal bin direcotry, or in a system direcotry like /usr/local/bin/ and being able to just enter:
or
Finishing up (finally!)
I have a personal bin directory, I use shebangs in all of my scripts and I don't typically use file extensions for ANY of my scripts.
When it comes to learning shellscripting, there are literally resources all over the internet!
All a shellscript is, is pretty much a bunch of shell commands strung together using a bit of shellscript syntax.
On Linux, the terminal is insanely powerful. The shellscripting language itself is relatively simple, but it is so versatile and expressive. You can string a bunch of different commands together and pipe, or redirect the output from one command to the input of another. You are only limited by your imagination.
A small part of shellscripting is learning the actual shellscripting syntax. But the largest part is learning to combine the vast multitude of existing programs/scripts and commands that we have available to us in our scripts.
As with anything, it takes a while to build up a working vocabulary of terminal commands. And very often there is more than one way to get something done. So using the terminal regularly and exploring and getting familiar with the commands available to us is a good idea. Exploring the man and info pages, to discover what options are available to us for any given command is also worthwhile. Sometimes the man pages can be a bit of a heavy read. But it's often worth it, because you can find some insanely useful bits of obscure functionality in some of the commands.
Start small and gradually work your way up to bigger things. Again, there are resources all over the internet for learning bash/shellscripting and Linux terminal commands.