Shell scripts allow you to give instructions to your operating system. Unix uses Bash as a shell, a command processor that ‘runs in a text window, where the user types commands that cause actions‘. Great! And here’s the part that interests us: ‘Bash can also read commands from a file, called a script‘.
Create a script
So let’s create a simple shell script and learn how to interact with our system! We’ll start with the simple display of a text line in the command window. To create it use a command line text editor (like Nano or Vim) or GUI applications (Mousepad, Pluma, Gedit…) Our shell script will have to be executable and will always begin with the interpreter directive #!/bin/bash.
#!/bin/bash echo "Heavy is the cinder block of Reality on the Strawberry pie of our illusions."
Save your text file (here: ‘reality_script’). You don’t need to give it an extension since the interpreter directive already give the info. The script is almost ready to run, you just need to 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.
# Permission 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 0 none
So, you’ve already guessed how to chmod our file:
chmod 755 reality_script
You can quickly execute the file by typing one of these two commands in your terminal:
./reality_script sh reality_script
The best way to execute your scripts however would be to store them in a /user/Scripts folder and put it in your PATH, to be executed in your terminal without specifying their directory or nature.
For the current session only:
Permanently, by adding the export line to your .bashrc:
echo 'export PATH=$PATH:/home/user/Scripts' >> /home/user/.bashrc
Just type ‘reality‘ 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:
#!/bin/bash pkill transmission
Do you need to execute a specific program with parameters, let say your music player in command line?
#!/bin/bash xfce4-terminal -x mocp -m /home/user/Music -T transparent-background
Try to fasten your daily operations with this great tool.