Linux Basic Series: Text Editors and Shell Scripting (P6)

This article explains how to use editors to create and modify files, shell scripting and bash shell script.

This is the 6th part of 7 part series where I explain the Basics of Linux.

Using text editors to create and modify files

Text editors are essential tools for creating and modifying files on a Linux system. One popular text editor is Vim, which is available on most Linux distributions. To use Vim, you can open a terminal and type 'vim' followed by the name of the file you want to create or edit. For example, to create a new file called 'my_file.txt' with Vim, you could use the following command:

vim my_file.txt

This will open Vim and display a blank file. From here, you can use the keyboard to navigate, edit, and save the file.

To install vim in Ubuntu use:

sudo apt install vim

Basic Linux Commands

The most simple commands allow you to open and close documents as well as save them. As with most other text editors, there are protections in place to help you avoid exiting the editor without having saved what you're working on.

Note: Press ESC Key in the editor first to enter below mentioned commands.

:help [keyword]- Performs a search of help documentation for whatever keyword you enter

:e [file] - Opens a file, where [file] is the name of the file you want to be opened

:w - Saves the file you are working on

:w [filename] - Allows you to save your file with the name you've defined

:wq - Save your file and close Vim

:q! - Quit without first saving the file you were working on


Introduction to Shell Scripting

Shell scripting is a powerful tool for automating tasks on a Linux system. A shell script is a program written in the Linux shell, which is a command-line interface for interacting with the operating system. Shell scripts can be used to perform a wide variety of tasks, from simple file management to complex system administration. To create a shell script, you can use a text editor like Vim to write the code and then save it with a '.sh' file extension. For example, to create a shell script called 'my_script.sh' with Vim, you could use the following command:

vim my_script.sh

This will open Vim and display a blank file. From here, you can write the code for your shell script and save it using the wq command.


Creating and running a basic shell script

To create and run a basic shell script with Vim, you can follow these steps:

  1. Open Vim and create a new file with the '.sh' file extension:

     vim my_script.sh
    
  2. Add the code for your script. For example, the following script will print 'Hello, World!' to the terminal:

     #!/bin/bash
     echo "Hello, World!"
    
  3. Save the file and exit Vim using the wq command.

  4. Make the file executable using the chmod command:

     chmod +x my_script.sh
    
  5. Run the script using the './' command:

     ./my_script.sh
    

This will execute the script and display 'Hello, World!' in the terminal.

Overall, text editors like Vim and shell scripting are powerful tools for working with Linux. By mastering these tools, you can become more efficient and productive on the Linux command line.