Getting Started
A shell script is a text file so should be created using a text editor. Do not use a word-processing program such as LibreOffice Write. Shell scripts should always be created on the operating system (Linux, macOS) on which they will run.
The first line should be
#!/bin/bash
The #!
is often called a shebang.
After the shebang, we can add some shell commands.
Example
Here is a simple example of a script that clears the screen, pauses for three seconds, then displays a quote from a Website.
#!/bin/bash
clear
sleep 3
echo -e "Here is a quote of the day:\n\n"
qod=`curl -s http://quotes.rest/qod.json | jq -r .contents.quotes[0].quote`
echo " " $qod
echo ""
If you have downloaded the file onto a Linux system, you can run it directly. Otherwise, either transfer it to your intended system or copy and paste it into a plain text file named qod.sh
.
Linux shells are indifferent to file suffixes. The sh
is a convention, but other suffixes may be used; for example, when writing a shell script for a resource manager such as Slurm, we may wish to use a .slurm
suffix.
To run the script, type
bash qod.sh
This invokes bash
to execute your script. Alternatively, you can modify your script to make it executable and run it:
chmod 755 myscript.sh
./quod.sh