PDF download Download Article PDF download Download Article

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.

1

Open a Terminal window.

PDF download Download Article
  1. 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 .
  2. Advertisement
2

Create a file with your text editor.

PDF download Download Article
  1. 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.
3

Begin your script.

PDF download Download Article
  1. #!/bin/bash . This is known as a Shebang line.
  2. Advertisement
4

Type the code.

PDF download Download Article
  1. 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 .
5

Save and exit your editor.

PDF download Download Article
  1. 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
  2. Advertisement
6

Make the script executable.

PDF download Download Article
  1. 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.
7

Run your shell script.

PDF download Download Article
  1. Type ./ListDir.sh to run this script. You should get the list of directories and the message as shown in the above image.
  2. Advertisement

Community Q&A

Search
Add New Question
  • Question
    After typing in "sudo apt install", "(sudo) password for jenneta" appears. What do I do?
    Living Concrete
    Top Answerer
    It is prompting you for the password to your account. Type in your password and then press Enter.
  • Question
    How do I learn shell scripting?
    Aryaa Vashisth
    Community Answer
    Install Python shell scripting and watch tutorials of how to script on Python.
  • Question
    How? I would like to stop the "file already exists" function at the OS level and instead rename the file on the disk before writing whatever to the original file name specified.
    Nithik R
    Community Answer
    You may have a file of the exact same name and that is why it shows up. Try using another name for your shell script "vim " Or if you want to rename an existing file, do "mv " without the quotes and the angular brackets.
Ask a Question
      Advertisement

      Tips

      Show More Tips
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      Warnings

      • For starters, place your shell scripts in your home directory as you begin your shell script journey. Once comfortable, place them in your required directory system.
      • Ensure that the files you create, do not reside in any system directories of the / = root file system.
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 295,375 times.

      Is this article up to date?

      Advertisement