In this tutorial, we will learn how to write a simple script in the Linux operating system. The most important use of scripting is Automation. Bash scripting allows you to automate tasks that you run commands using the console. We can even schedule the scripts in crontab to run unattended automatically.
Simple Bash Script
Let’s write a simple bash script. We use Open Suse Linux distribution in this tutorial.
Go to konsole or Terminal
Create a directory to hold your scripts.
$> mkdir scripts
Change to the directory and create a file with .sh extension.
$> cd scripts/
$> touch sampleScript.sh
To edit the file, we use vi editor. Type the following command.
$> vi sampleScript.sh
Enter into the insert mode by hitting i key button.
Type the sample script code as shown below. The script prompts the user for a name and then echos
it back to the console with a greeting.
Hit the escape button and:wq to save and exit the vi editor.
#!/bin/bash # This is a simple Linux script # Greeting echo -e "Enter your name:= " read fullName echo "Hello! $fullName"
Try to run the script file with ./
bash: ./sampleScript.sh: Permission denied
Notice the Permission denied error message.
We need to provide the execution permission for the user to execute the shell script.
> chmod +x sampleScript.sh
> ls -l sampleScript.sh
-rwxr-xr-x 1 suri users 123 Apr 21 05:43 sampleScript.sh
Once you have granted the execute permission with the chmod command. We can execute the shell script.
That’s it. We have run a simple shell script on a Linux box!