A shell script is a text file containing one or more commands.
# My example bash script (the comment line above ensures this will run under bash)
echo "Hello World"
Next you need to make the script executable with chmod
$ chmod u+x my_script1.sh
You can now run the script by prefixing it with ./
If you will be writing a few shell scripts then it's worth creating a folder, perhaps called "scripts" and adding that to the system path:
$ mkdir -p $HOME/scripts
$ export PATH="$PATH:~/scripts"
Even better is to edit your .bash_profile file to include export PATH="$PATH:~/scripts" that will keep the "scripts" folder in your path every time you log in.
With the script saved in the folder, you can now run it with just:
Passing parameters to a shell script:
$ cat myscript2.sh
echo 'Welcome' $0 'says' $1 $2
$ chmod a+x myscript2.sh
$ myscript2.sh Hello world
Welcome myscript2.sh says Hello world
A shell script is a text file containing shell commands. When such a file is used as the first non-option argument when invoking Bash, and neither the `-c' nor `-s' option is supplied, Bash reads and executes commands from the file, then exits. This mode of operation creates a non-interactive shell.
A shell script may be made executable by using the
to turn on the execute bit. When Bash finds such a file while searching the
$PATH for a command, it spawns a subshell to execute it.
In other words, executingfilename arguments
is equivalent to executingbash filename arguments
filenameis an executable shell script. This subshell reinitializes itself, so that the effect is as if a new shell had been invoked to interpret the script, with the exception that the locations of commands remembered by the parent are retained by the child.
Most versions of Unix make this a part of the operating system's command execution
mechanism. If the first line of a script begins with the two characters `#!',
the remainder of the line specifies an interpreter for the program. Thus, you
can specify Bash,
awk, Perl, or some other interpreter and write
the rest of the script file in that language.
The arguments to the interpreter consist of a single optional argument following the interpreter name on the first line of the script file, followed by the name of the script file, followed by the rest of the arguments. Bash will perform this action on operating systems that do not handle it themselves. Note that some older versions of Unix limit the interpreter name and argument to a maximum of 32 characters.
Bash scripts often begin with
#! /bin/bash (assuming that Bash
has been installed in /bin ), since this ensures that Bash will be
used to interpret the script, even if it is executed under another shell.
When Bash runs a shell script, it sets the special parameter
to the name of the file, rather than the name of the shell, and the positional
parameters are set to the remaining arguments, if any are given. If no additional
arguments are supplied, the positional parameters are unset.
/bin/bash^M: bad interpreter: no such file or directory
This usually means the file has been edited and has Windows <CR-LF> instead of unix <LF> line endings
Windows equivalent command: CMD - Start a new CMD shell