Linux shell scripts are the equivalent of windows batch scripts, they are really no more than a series of commands in a text file. In particular, they have a call to the shell they are supposed to use (linux has a number of different shells available, with different functionalities and syntaxes), which will look like
#!/bin/bash
The #! tells the system that what follows is the path to the shell that should run the commands contained in the script. /bin/bash is the location of the bash shell, the linux standard. Take for example the series of commmands:
ls
pwd
whoami
The first command, ls, gives a listing of files in the current directory. The pwd command prints the path of the directory you are currently located in. The whoami command returns your username. A shell script to execute those commands would look like this:
Code:
#!/bin/bash
ls
pwd
whoami
So far it's just a text file; it needs to have execute permission added to it. The command
Where 'shellfile' is the name of the script, the file containing the series of commands. The script can now be executed by passing the command
from the directory where the script resides. You can also pass the absolute path, for example /home/bob/shellfile. The results and output of the commands in the script will be displayed on your screen.
Now, I kind of lied when I said that it
needs execute permission to be run; an alternative to adding execute permission to a file can be to use the source command, as in
source shellfile
or
. shellfile
(the . is short for 'source'). You may also forego the inclusion of the #!/bin/bash call on the first line and invoke your script with
bash shellfile
Keep in mind too that ANYONE, including root, attempting to execute a file that does not have execute permission set (either by ./shellfile or /home/bob/shellfile) will get a "permission denied" error. Many shell script files will also be named with a .sh extension, or one that corresponds to the shell that handles them, such as .ksh, .bash, .zsh, etc, although this is not strictly necessary.