Linux Shell – Getting Started

Published by on August 24, 2018 in Net Admin

Linux Shell Getting Started

When it comes to Linux, the Linux Shell is the backbone of your interaction with your system. Whether you’re looking to execute commands, load up programs or simply delete files the shell is your closest ally. In this article we’re going to look at the Linux Shell and how you can use the Command Line Interface (CLI) to get the most out of your distribution.

What is the Linux Shell?

The Linux Shell is a command line interpreter that takes the commands you enter from the command line and hands them over to the operating system. Originally there was only one shell interface available on Linux. However, today there are many different ones. Most systems today use the Bourne Again Shell (bash) which is a more advanced version of the original shell program which was referred to as sh. Though you’re most likely to encounter bash there are a number of alternative shell programs such as ksh, tcsh and zsh.

A guide to the shell programs can be seen below:

  • Bourne Shell (sh) – Basic Unix Shell on most systems
  • Korn Shell (ksh/pdksh) – An enhanced version of the Bourne Shell
  • C Shell (csh) – Syntax similar to the C programming language
  • Bash Shell (bash) – This shell combines the Korn Shell and the C Shell (default on Most Linux)
  • tcsh (tsch) – Similar to the C Shell

In order to change shells you need to run a shell command. This will change the shell that loads up when you log in. To change shell for the next time you log in you would use the chsh command. You can run this by entering the following:

chsh

You will then be shown a message similar to the following:

Current available shells:

/usr/bin/sh
/usr/bin/bsh
/usr/bin/csh
/usr/bin/ksh:
Current login shell:
/usr/bin/ksh
Change (y/n)? >

You can choose what you’d like to change to by pressing y to make the change. Once the to? prompt appears you would enter the next shell you want to use. For example:

change (y/n)? > y 
to? > /usr/bin/csh

The chsh command will change the shell program that runs after you log in. It is important to note that this command only changes the login shell for the user who issued the command (under the default settings). This command is interactive and you will be shown a list of alternative shells to choose from. To change the shell you’ll need to enter the full path name.

There are also many different terminal clients for you to use to assess the shell such as xterm, konsole and lxterm. However, in many cases it may also be called Terminal Emulator.

Shell Prompt Guide

When you log in to the shell you’re going to be prompted to enter a command via one of the following symbols; $, % and #. The default prompts for Bourne, Korn and Bash are $. For the C Shell % is the default prompt. One of the most important things to note about logging in is that if you are logged in as an administrator under the root username, then a stray command can damage your installation files. Luckily, it is easy to remember when you’re logged in as a root user as the prompt is # instead.

Keyboard shortcuts

Using keyboard shortcuts helps you to use the Linux Shell more quickly. Commands are as follows:

  • CTRL + U – Text cut from the left hand side up to the cursor
  • CTRL + K – Cuts text from the right of the cursor until the end of the line
  • CTRL + Y – Pastes Text
  • CTRL + E – Moves cursor to the end of the line
  • CTRL + A – Moves cursor to the start of the line
  • ALT + backspace – Deletes previous word
  • ALT + F – Jumps to next space
  • ALT + B – Jumps to the previous space
  • CTRL + W – Cuts the word before the cursor
  • Shift + Insert – Paste

Beginner Shell Commands

  • cd – Changes the current working directory
  • man – This will provide you with more information on commands and Linux
  • sudo – Allows you to run commands as a root (with no permission limitations)
  • exit – Quits the current program, exits the command line terminal or logs you out completely (based on context)
  • Kill   Stops processes like “End Process” on Windows Task Manager
  • Is  Lists the directory content of specified files and directories (no directory specified = current directory_
  • pwd – Shows working directory of the command line terminal
  • reboot – Stops all running programs, closes down and then reboots

Navigate Directory

CodeAction
lsLists files in the current directory
ls -alList files in the current directory (includes dot files)
cd dirpathChange directory
cdGo to $HOME directory
cd ..Goes to the next directory

File Directory and File Manipulation

CodeResult
touch filenameCreates a new file or updates timestamp
mkdir new_dir_nameCreates a new directory
rm filenameDeletes a file
rm -r dirnameDeletes a directory
rmdir dirnameDeletes an empty directory
cp filename new_filenameCopy a directory
mkdir new_dir_nameCreates a new directory
mv filename new_nameRenames a file or moves it to a different directory

Viewing Files

CodeOutcome
cat fnameShow file content
cat fname  | moreView a file by page
vi fnameView file
head fnameShow starting lines of a file
tail fnameShow last lines of a file
tail -f fnameShows last lines of a growing file
file fnameShows file type

Shell Scripts

As you can see there are a ton of different commands for Linux Shell and these are just the tip of the iceberg. However, what you might not know is that you can create shell programs to enter multiple commands automatically. For instance you can use store multiple commands within a txt file and run a shell program to use those commands without having to type them. The main advantage of this is that it makes your user experience faster and more convenient.

To create a script you need to use a text editor to write your commands in ASCII text. We’ve listed a number of text editors below to help you out.

Command Line Text Editors

  • vi,vim – vi is one of the most famous text editors for offering a lean experience that’s incredibly fast. However, today you’re most likely to encounter vim (an updated version of vi).
  • nano – nano is a command line interface that comes with an email program called pine. It doesn’t offer much in the way of additional features which makes it a good choice for users who are looking to keep things simple.
  • Emacs – Emacs is one of the most full-featured text editors you’ll find. This is one of vi’s most popular competitors.

GUI Text Editors

  • gedit – gedit is a GUI-based text editor that comes alongside Gnome
  • kwrite – kwrite is a more advanced text editor with syntax highlighting. This program comes with KDE

Once you’ve selected what text editor you want to use, you can start to type a script. Open up your text editor and enter the following:

#!/bin/bash
# My first script 
echo “special text!”

The lines of the script mean the following:

  1. shebang – The first line of the script dictates what program will be used to interpret the script. In the example above /binbash has been used (but you can also use Perl, aw, tcl, Python and Tk instead.
  2. comment – The comment basically explains to the programmer viewing the text what the script is for (the # tells  bash to ignore what appears after it.)
  3. echo command – Prints out the argument.

Granting a Script Permission

Once you’ve typed up a script you need to give it permission in order to work. To do this you need to use the chmod command:

$ chmod 755 special_text

The number 755 will give you permission to read, write and execute scripts. Alternatively you can use 700 if you want to restrict yourself to read and execute only. Once you’ve done this, your script will be ready to run. You can run your script by entering the following:

$ ./special_text

In most cases this will raise your script. If you don’t see the directory you saved your script to then you’ll need to go and find which directory you have your script saved in.

This is where a path comes in. The path is essentially a list of directories that the shell searches when prompted. If your script isn’t within the path then you will see a command not found error message.

To view the list of directories within the path enter the following:

echo $PATH

This will show you a list of directories the shell will search if you don’t provide a command name when you enter the command. You can add directories to this list with the following command:

$ export PATH=$PATH : directory

If you’re on a distribution which has specific directories for each user it is referred to as a bin (a subdirectory of your home directory). You can create a bin with the following command if you don’t have one:

$ mkdir bin 

Once you’ve moved your script to the bin directory you’ll be able to run without issue.

Why Do I Need Shell Scripts?

There are many reasons to go through the process of writing your own scripts, and the number one reason is convenience. With shell scripts you can create your own commands and save time entering commands on a case-by-case basis. You can effectively automate multiple commands. Without scripts you’d have to run these manually yourself each and every time.

Best Linux Resources

Linux Resources

Of course if you’re new to Linux then you’re going to want to learn more. With Linux there is no better way to learn than by actually learning the system. However, you want to make sure you’re reading the latest content and watching additional tutorials to make sure your knowledge is up-to-date and top notch.

  • Linux.com – If you need to learn more about Linux then this site is one of the first you should visit. It not only offers a range of tutorials resources and news but  also has its own community forums as well.
  • LinuxCommand.org – When it comes to learning more about the Linux Shell, this website has to be one of the best online. It provides the basics you need to start using the shell and writing shell scripts. It also includes links to other useful resources like new_Script which is a script template generator.
  • Learn Linux in 5 Days and Level Up Your Career – This Udemy course is ideal for those who are looking to make strides in using Linux quickly.
  • Linux Newbie Guide – Linux Newbie Guide should be your first port of call if you’re new to Linux systems. This website will take you through what Linux is and how to choose a distribution that is right for you. There are also a variety of courses, tutorials, and video guides to help teach you more as you go.
  • Compute Freely – This website is targeted specifically towards Linux newbies. It provides guidance based on Linux distribution and contains a wide variety of instructions on distributions such as Debian, Fedora, Ubuntu, CentOS, and Puppy Linux. These are denoted as Beginner, Intermediate, or Advanced so that you can find the right one for your knowledge level.
  • Full Linux Tutorial: Go From Beginner To Advanced with RedHat Linux, CentOS, and Kali Linux! – This is an in-depth video tutorial that aims to help beginners establish fluency with Linux. This tutorial has over seven hours of content and is well worth it if you want to learn quickly.
  • Learn Unix The Hard Way – This course is designed to take you through configuring  Linux machines. You might want a little more practice before jumping straight into this because it is quite complex compared to some other resources.

Linux Shell: Resources Abound  

As you can see the Linux Shell is quite a complex topic. The beauty of Linux is that there are many different distributions, shells, and text editors at your disposal. However, this cuts both ways and can be quite difficult to get your head around when you’re starting out. While it can be tempting to go it alone and experiment we recommend that you seek the guidance of established resources so that you learn without running into any issues.

Training yourself how to use Linux Shell is definitely possible but you want to make sure you’re using a variety of resources to make sure you’re learning the right way. This way you’ll be able to learn quickly without running into plateaus that slow your progress down to a crawl.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.