Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left
Apply NowLogin using Social Account
     Continue with GoogleLogin using your credentials
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:
#!interpreter [arguments]
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 myfirstscript.sh
:
nano myfirstscript.sh
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+X
to save and then Y
myfirstscript.sh
. If not enter the same filename.Hit 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:
./myfirstscript.sh
Taking you to the next exercise in seconds...
Want to create exercises like this yourself? Click here.
Note - Having trouble with the assessment engine? Follow the steps listed here
Loading comments...