What is bash script?

Bash is a Unix shell, which is a command line interface(CLI) for interacting with the operating system(OS).

Bash script which contains command lines can be run in Bash and finish extremely excellent work.

Prepared work

Create a bash script

In Linux operating system, suffix name is not necessary so that we can name the bash script whatever we want. For example, we can create a folder named scripts and
a bash script named experiment in it.

1
2
3
mkdir scripts
cd scripts
touch experiment

In order to access scripts in this folder anywhere, adding this directory to ENV PATH is recommended.

1
2
# optional
export PATH=/path/to/scripts:$PATH

Make the bash script executable

To run the experiment script, it’s necessary to change its mode.

1
chmod u+x experiment

Add symbol

Each bash script should begins with the following line for specifying the interpreter.

1
#!/bin/bash

Basic syntax

Echo

Keyword echo can be used to output contents to the screen. Add the following line in experiment script and run the script, Hello world can be seen in the terminal.

1
echo Hello world

Variables

Variables must be declared before being invoked. Use = to assign variables and use $ to call them.

1
2
3
4
#!/bin/bash
name="Jack"

echo Hello, I am $name

Read

read can be used to assign variables from input on ternimal.

1
2
3
4
5
6
#!/bin/bash
echo what are you?

read who

echo Hello, $who

Conditions

Structures like for, if...else can also be used in bash scripts. But operators used in bash are different from those in programming languages.

Judgment

Bash Operators Operators Description
-eq == equal
-ne != not equal
-gt > greater than
-ge >= greater than or equal
-lt < less than
-le <= less than or equal
-z == null is null

For example:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
echo How much is it?

read prize

if [ $prize -gt 100 ]
then
echo Too expensive
else
echo Great
fi

Looping

Looping is another powerful structure in programming language, we also can use it in bash scripts as follow.

1
2
3
4
5
6
7
#!/bin/bash
FILES=/opt/*

for file in $FILES
do
echo $(basename $file)
done

Open another terminal

Sometimes multiple screens are required to display different messages. To satisfy this requirment, gnome-ternimal can be used to open another ternimal and run commands in it.
Add gnome-terminal in a bash script, a new ternimal will open when we run the script.

1
2
#!/bin/bash
gnome-ternimal

Optional arguments

Some arguments are used for customizing the new ternimal.

  • --maximize: Maximize the ternimal
  • --full-screen: The terminal Occupy the full screen after opening.
  • --window --window: Open two terminals
  • --window --tab --window --tab --tab: Open two ternimals and open tabs in them
  • --geometry=80x25+10+10: Set size of the terminal
  • -e: run the following commands in the new terminal, the following commands should be included in ‘’.

Fro example:

1
2
#!/bin/bash
gnome-terminal --geometry=80x25 -e 'echo $PATH'

However, the terminal will disappear after executing commands. In order to retain the terminal after executing commands, we can use the following commands to maintain the window.

1
2
#!/bin/bash
gnome-terminal --geometry=80x25 -e 'bash -c "echo $PATH;exec bash"'

References