Skip to content




Linux »

Shell Scripts

A shell is a command line interpreter that interprets the command line input and instructs the operating system to perform any necessary tasks and commands. Shell commands can be put into scripts to do multi-stages and more complex works.

Last update: 2022-06-04


Unix Shells#

A shell is a command line interpreter that interprets the command line input and instructs the operating system to perform any necessary tasks and commands. Shell commands can be put into scripts to do multi-stages and more complex works.

  • Bourne Shell (sh): a historically first shell
  • C Shell (csh): an C style shell
  • Korn Shell (ksh): a mixed of Bourne Shell and C Shell
  • Bourne Again Shell (bash): an GNU version of Korn Shell
  • Debian Almquist shell (dash): a smaller Bash shell
  • Tenex-extended C Shell (tcsh): an Advanced C Shell
  • Z shell (zsh): the most advanced shell

To see available shells in the current system:

cat /etc/shells
User default shell

The file /etc/passwd list all username, groups, and login shell.

For example, the root account use bash shell:

cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash

At runtime, to get current shell:

ps -p $$
# or
echo $0
Change shell

To change shell at runtime, Type the name at the command line and then press the enter key. In this example, to change from any shell to the bash, type:

bash

To change default shell for a user, use chsh command.

chsh -s <shell>
# or
sudo chsh -s <shell> <user>

Shell Environment#

Environment variables save the properties of the working shell environment.

To view environment variables:

env
# or
printenv

You can see some familiar variables: PATH, HOME, SHELL, PWD, LANG, etc.

Shell scripts#

A bash script is a series of commands written in a file. These are read and executed by the bash program. The program executes line by line.

By naming conventions, bash scripts end with a .sh. However, bash scripts can run perfectly fine without the sh extension.

Scripts are also identified with a shebang. Shebang is a combination of bash # and bang ! followed the bash shell path. This is the first line of the script. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

Below is an example of the shebang statement.

#! /bin/bash

Scripts have execution rights for the user executing them. Set the execution flag with chmod command:

chmod +x script.sh

Example script to read user input:

#!/bin/sh

echo "What is your name?"
read PERSON
echo "Hello, $PERSON"

Another example: Startup Scripts

  • /etc/profile: run first for all users during the login process
  • $HOME/.bashrc: run for individual user’s customized setup when executing bash

Advanced Bash-Scripting Guide

An in-depth exploration of the art of shell scripting available at https://tldp.org/LDP/abs/html/.

Exercise#

The below script randomly creates many .txt files:

#!/bin/bash
for i in {1..10}
do
    num=`expr $RANDOM % 4`
    case $num in
        0) touch ./RIGHT_CAT_$i.txt;;
        1) touch ./RIGHT_DOG_$i.txt;;
        2) touch ./WRONG_CAT_$i.txt;;
        3) touch ./WRONG_DOG_$i.txt;;
    esac
done

You have to write a script to do:

  • Replace WRONG to RIGHT in filename. If the target filename already exists, append _new to the filename.

    For example:

    Change WRONG_CAT_1.txt to RIGHT_CAT_1_new.txt when RIGHT_CAT_1.txt exists.

  • Create 2 folders CAT and DOG and move all files to corresponding folders

Comments