How to Write a Shell Script Using Bash Shell in Ubuntu

Launch the Terminal., In the Terminal launch the vi/vim editor., In the terminal window, type vim ListDir.sh and hit enter., At the top type the following code #!/bin/bash., Type the code as shown in the figure., Type the following key combinations...

11 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 1: Launch the Terminal.

    You can launch the terminal by opening the dash and searching for it by name.

    You could also open it by using the keyboard shortcut Ctrl + Alt + T.
  2. Step 2: In the Terminal launch the vi/vim editor.

    Vim is a free and popular text editor that uses the command line interface.

    To install Vim, type "sudo apt-get install vim". , Once inside the Editor, type "I" or "i".

    This will allow you to insert / start typing your shell script. , This is known as a Shebang line. , 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. , This will write the changes to the file and bring you back to the terminal.

    Save the file as ListDir.sh ,, You should get the list of directories and the message as shown in the above image. ,
  3. Step 3: In the terminal window

  4. Step 4: type vim ListDir.sh and hit enter.

  5. Step 5: At the top type the following code #!/bin/bash.

  6. Step 6: Type the code as shown in the figure.

  7. Step 7: Type the following key combinations

  8. Step 8: Esc + : + wq to escape the editor.

  9. Step 9: 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.

  10. Step 10: Type ./ListDir.sh to run this script.

  11. Step 11: Congratulations on writing your 1st Shell Script using Bash in Ubuntu Linux!

Detailed Guide

You can launch the terminal by opening the dash and searching for it by name.

You could also open it by using the keyboard shortcut Ctrl + Alt + T.

Vim is a free and popular text editor that uses the command line interface.

To install Vim, type "sudo apt-get install vim". , Once inside the Editor, type "I" or "i".

This will allow you to insert / start typing your shell script. , This is known as a Shebang line. , 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. , This will write the changes to the file and bring you back to the terminal.

Save the file as ListDir.sh ,, You should get the list of directories and the message as shown in the above image. ,

About the Author

J

Joshua Cole

Specializes in breaking down complex practical skills topics into simple steps.

163 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: