A shell script is a file containing a list of shell commands which would otherwise could be run individually on the OS shell prompt.
Note: In Unix, the extension doesn't dictate the program to be used while executing a script. It is the first line of the script that would dictate which program to use. The first line starts with the sequence of two characters #! that is called shebang. This is followed by the program that tells the operating system which interpreter to use to parse the rest of the file.
The Shebang interpreter directive takes the following form:
In the example below, the program is
/bin/bash which is a Unix shell.
Let's create a simple command that prints two words:
Open a text editor to create a file
Write the following into the editor:
#!/bin/bash name=linux echo "hello $name world"
If you want that every variable must be declared before it can be used, then add set -u at the top of the script after the shebang line.
#!/bin/bash set -u name=linux echo "hello $name world" echo $surname
Ctrl+Xto save and then
myfirstscript.sh. If not enter the same filename.
Enter key to exit
Now, by default, it would not have executable permission. You can make it executable like this:
chmod +x myfirstscript.sh
To run the script, use:
Note - Having trouble with the assessment engine? Follow the steps listed here