Shell scripts allow you to give instructions to your operating system. In this post we’ll use Zsh as a shell (see here how to install it if it doesn’t come with your distro), a Unix shell that can be used as an interactive login shell and as a command interpreter for shell scripting.
Create a script
How to create a simple shell script and learn to interact with our system. We’ll start with the simple display of a text in the command window.
To create your text, use a command line text editor (like Nano or Vim) or GUI applications (Mousepad, Gedit…) The shell script will have to be executable and will always begin with the interpreter directive #!/bin/bash.
echo "Heavy is the cinder block of Reality on the Strawberry pie of our illusions. (© Boulet)"
Formatting tip : you can use bash scripting even if using Zsh as an interpreter. In order to apply a style on your string, use these commands:
> echo -e '\033[1m Text'
- echo -e — The
-eoption means that escaped (backslashed) strings will be interpreted
- \033 — escaped sequence represents beginning/ending of the style
- lowercase m — indicates the end of the sequence
- 1 — Bold attribute (see below for more)
- [0m — resets all attributes, colors, formatting, etc.
The possible integers are:
0 — Normal Style
1 — Bold
2 — Dim
3 — Italic
4 — Underlined
5 — Blinking
7 — Reverse
8 — Invisible
Black 0;30 Dark Gray 1;30
Red 0;31 Light Red 1;31
Green 0;32 Light Green 1;32
Brown/Orange 0;33 Yellow 1;33
Blue 0;34 Light Blue 1;34
Purple 0;35 Light Purple 1;35
Cyan 0;36 Light Cyan 1;36
Light Gray 0;37 White 1;37
Save the text file (here: ‘parpaing’). You don’t need to give it an extension since the interpreter directive already give the info.
The script is almost ready to run, change its access permissions and make it executable. Use the chmod command followed by three digits: they will refer to the permissions for the file owner, the group, and other users.
7 read, write and execute
6 read and write
5 read and execute
4 read only
3 write and execute
2 write only
1 execute only
➜ ~ chmod 755 parpaing
You can quickly execute the file by typing:
➜ ~ sh parpaing
The best way to execute your scripts however would be to store them in a /user/bin folder and put it in your PATH, to be executed in your terminal without specifying their directory or nature.
Simply add the export line to your .zshrc file:
Now, type ‘parpain’ in your terminal, from any location, to launch the script.
You can imagine many uses for programs like these. Do you need to switch off Transmission once your totally legit torrent has ended? Create and load this one on the app interface:
Try to speed up your daily operations with this tool.