In Linux, shell scripts help us in so many different ways including performing or even[automating certain system administration tasks][1], creating simple command line tools and many more.
In this guide, we will show new Linux users where to reliably store custom shell scripts, explain how to write custom shell functions and libraries, use functions from libraries in other scripts.
### Where to Store Shell Scripts
In order to run your scripts without typing a full/absolute path, they must be stored in one of the directories in the$PATHenvironment variable.
Normally, if the directorybinexists in a users home directory, it is automatically included in his/her$PATH. You can store your shell scripts here.
Therefore, create thebindirectory (which may also storePerl,[Awk][2]orPythonscripts or any other programs):
```
$ mkdir ~/bin
```
Next, create a directory calledlib(short for libraries) where you’ll keep your own libraries. You can also keep libraries for other languages such as C, Python and so on, in it. Under it, create another directory calledsh; this will particularly store you shell libraries:
```
$ mkdir -p ~/lib/sh
```
### Create Your Own Shell Functions and Libraries
Ashell functionis a group of commands that perform a special task in a script. They work similarly to procedures, subroutines and functions in other programming languages.
The syntax for writing a function is:
```
function_name() { list of commands }
```
For example, you can write a function in a script to show thedateas follows:
```
showDATE() {date;}
```
Every time you want to displaydate, simply invoke the function above using its name:
```
$ showDATE
```
Ashell libraryis simply a shell script, however, you can write a library to only store your functions that you can later call from other shell scripts.
Below is an example of a library calledlibMYFUNCS.shin my~/lib/shdirectory with more examples of functions:
```
#!/bin/bash
#Function to clearly list directories in PATH
showPATH() {
oldifs="$IFS" #store old internal field separator
IFS=: #specify a new internal field separator
for DIR in $PATH ; do echo $DIR ; done
IFS="$oldifs" #restore old internal field separator
}
#Function to show logged user
showUSERS() {
echo -e “Below are the user logged on the system:\n”
w
}
#Print a user’s details
printUSERDETS() {
oldifs="$IFS" #store old internal field separator
IFS=: #specify a new internal field separator
read -p "Enter user name to be searched:" uname #read username
echo ""
#read and store from a here string values into variables using : as a field delimiter
In this article, we showed you where to reliably store shell scripts, how to write your own shell functions and libraries, invoke functions from libraries in normal shell scripts.
Next, we will explain a straight forward way of configuringVimas anIDEfor Bash scripting. Until then, always stay connected to TecMint and also share your thoughts about this guide via the feedback form below.
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.