How to Write a Shell Script Using Bash Shell in Ubuntu
How to Write a Shell Script Using Bash Shell in Ubuntu
Have you ever wanted to automate operations in your operating system? This wikiHow article teaches you how to write a shell script using Bash Shell in Ubuntu.
Steps

Open a Terminal window.

Launch the Terminal. You can launch the terminal by opening the dash and searching for it by name. You could also open it using the keyboard shortcut Ctrl+Alt+T.

Create a file with your text editor.

Launch the vi or vim editor and create a new script. Vim is a free and popular text editor that uses the command line interface. To create a script called ListDir.sh: Type vim ListDir.sh and hit ↵ Enter. Once inside the Editor, type "I" or "i." This will allow you to insert/start typing your shell script.

Begin your script.

Type the following code: #!/bin/bash. This is known as a Shebang line.

Type the code.

Type the code as shown in the figure. The first line (echo "Welcome") prints the line "Welcome" into the terminal. echo is used to return the given text and a new line. The second line (ls) lists the contents of the directory. ls is short for list. The final line (another echo statement) returns the text This completes the list of directories.

Save and exit your editor.

Exit vim. Type Esc, then :, and then + wq to save and quit. This will write the changes to the file and bring you back to the terminal. Save the file as ListDir.sh

Make the script executable.

Change the permissions. Enter the following command: chmod +x ListDir.sh. chmod is a command used in Unix like operating systems to change the access permissions to a file.

Run your shell script.

Run the script from the command line. Type ./ListDir.sh to run this script. You should get the list of directories and the message as shown in the above image.

What's your reaction?

Comments

https://umatno.info/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!