A script is used in Linux and contains commands written according to work specifications and assignments. When executed, each command in the script executes in order. The shell is the user-written command interpreter. A Shell script helps a user write and execute multiple commands at the same time.
This article will show how to execute shell scripts through command line input. Concerning this particular article, I've tested it on Ubuntu 24.04, Ubuntu 22.04 and Ubuntu 20.04. Before demonstrating how to execute a shell script through CLI, we will first see how to create a shell script.
To create a “.sh” file, follow the following steps:
You can either use the default text editor in Ubuntu or if there is any other editor installed in your system for example “vim text editor”. For this particular guide, I am using the default text editor.
Type “editor” and click on the ”Text Editor”.
The default text editor will be open.
You can write any commands as per your work requirements or assigned tasks. I am writing the following echo commands/code in the untitled document for this particular article.
echo "Hello World" echo "Ubuntu 20.04 LTS tutorial" echo "Today’s task"
Now save the file with the ".sh" extension in the directory of your own choice. For this particular article, I am saving it in the default folder - the home folder - and the file is named "tutorial.sh".
Now, the script "tutorial.sh" will look something like this in the text editor after being saved.
You have to execute the shell script through command line input.
First, you have to make the shell script executable by running the following command:
$ chmod +x scriptname
Write your shell script name in place of “scriptname” in the above command. For this particular guide, the script name is “tutorial.sh”.
Run the script using the following command:
$ ./scriptname
The script name is “tutorial.sh” as mentioned above.
On running the above command, it will execute each line of script one by one and display the following output:
A shell script in Ubuntu is a text file containing a series of commands that the shell can execute. It's a way to run multiple commands automatically, saving time and effort.
A basic shell script starts with #!/bin/bash on the first line, which tells Ubuntu to use the Bash shell to interpret the script. Following lines contain the commands you want to execute.
In the terminal, use the chmod command: chmod +x myscript.sh. This changes the script’s permissions, allowing it to be executed.
After making it executable, you can run the script by typing ./myscript.sh in the terminal from the directory where the script is located.
Yes, you can pass arguments to your script. Inside the script, arguments are accessed using `$1`, `$2`, etc., where `$1` is the first argument, `$2` the second, and so on.
Use the # symbol to start a comment. Everything on the line after # will be ignored by the shell.
bash (Bourne Again SHell) is an enhanced version of sh (Bourne Shell). Bash includes additional features like command line editing. If your script relies on Bash-specific features, use #!/bin/bash; otherwise, #!/bin/sh is sufficient.
Run your script with the -x option: bash -x myscript.sh` This prints each command and its arguments as they are executed, which is useful for debugging.
Yes, you can use cron, a time-based job scheduler in Unix-like systems. To add a script to cron, edit the crontab file with crontab -e and specify the time and script path.
Shell scripts can have `for`, `while`, and `until` loops, as well as `if-else` and `case` statements, making them quite powerful for automating complex tasks.
Use the read command. For example, read -p "Enter your name: " name will prompt the user to enter their name and store it in the variable name.
Some best practices include: adding comments for clarity, checking for errors after each command, using clear and descriptive variable names, and avoiding the use of hard-coded values where possible.
To make your script portable, avoid using Bash-specific features if not necessary, and instead use standard POSIX-compliant syntax. Also, explicitly define paths to binaries instead of assuming their location.
In many cases, yes, especially if you follow standard practices and avoid system-specific features. However, always test your script on the target system to ensure compatibility.
This article demonstrates the basic initials of shell script and how to create and execute a shell script in a very simple and easy-to-understand manner. The user can perform each step by looking through the attached screenshots of each input command and what will be the correct output of that command. The article will help both professional and naive users equally.
Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…
ISPConfig is an open-source control panel that allows users to manage multiple servers from a…
As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…
Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…
Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…
phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…