Fundamentals 11 min read

Master Shell Scripting: From Basics to Advanced Control Structures

This tutorial walks Java developers through the fundamentals of Linux shell scripting, covering how to create and run scripts, check and set the default shell, manage permissions, define and manipulate variables, and implement flow‑control constructs such as if/else, case, for, and while loops with practical code examples.

macrozheng
macrozheng
macrozheng
Master Shell Scripting: From Basics to Advanced Control Structures

Java developers often work on Linux servers, packaging projects as JAR or WAR files, uploading them via XFTP, and starting them with a service script; now they can learn how to write their own shell scripts.

Shell Script

A shell script is a program file that contains a series of commands interpreted by a command‑line interpreter (the shell). When commands are placed in a file instead of typed interactively, the file becomes a shell script.

Shell scripts can include assignments, calculations, loops, and other operations.

1. View the current default shell

<code>echo $SHELL</code>

Output:

/bin/bash

2. View all shells supported by the system

<code>cat /etc/shells</code>

Output:

/bin/sh /bin/bash /usr/bin/sh /usr/bin/bash

How to Write a Shell Script

Create a directory and a script file, then edit it:

<code>mkdir /usr/local/shelltest</code>
<code>touch test.sh</code>
<code>vim test.sh</code>

Example script (saved as

test.sh

)

<code>#!/bin/bash

echo "Hello World Shell"
</code>

Run the script:

<code>bash test.sh</code>

Result:

Hello World Shell

The line

#!/bin/bash

(shebang) tells the system to use

/bin/bash

as the interpreter.

To execute a script directly you must grant execute permission:

<code>chmod +x test.sh</code>

Then run it with

./test.sh

.

Shell Script Variables

Define a variable:

<code>name=zhiyikeji</code>

Use the variable with

$

:

<code>echo $name</code>
<code>echo ${name}</code>

Both produce

zhiyikeji

. Braces are optional but help avoid ambiguity.

Delete a variable:

<code>unset name</code>

After unsetting,

echo $name

outputs nothing.

Make a variable read‑only:

<code>readonly name</code>

Attempting to reassign a read‑only variable results in an error.

Shell Script Flow Control

If

<code>#!/bin/bash
if [ $1 -gt 2 ]; then
    echo "Value greater than 2"
else
    echo "Value less than or equal to 2"
    exit
fi
</code>

Run with

sh test2.sh 1

Value less than or equal to 2

;

sh test2.sh 3

Value greater than 2

.

Case

<code>case $1 in
    start)
        echo "start already begun"
        ;;
    stop)
        echo "stop command executed"
        ;;
esac
exit
</code>

Running

sh service.sh start

prints

start already begun

;

sh service.sh stop

prints

stop command executed

.

For Loop

<code>j=$1
for ((i=1;i<=j;i++))
do
    echo $i
done
</code>

Example:

sh fortest.sh 6

outputs numbers 1 through 6.

While Loop

<code>a=1
b=1
while ((a <= 9))
do
    while ((b <= a))
    do
        let "c=a*b"
        echo -n "$a*$b=$c "
        let b++
    done
    let a++
    let b=1
    echo ""
 done
</code>

Produces the classic 9×9 multiplication table.

Shell scripting is a practical skill for automating tasks on Linux servers; mastering these basics enables you to write simple automation scripts and lay the groundwork for more complex DevOps workflows.

AutomationLinuxbashVariablesshell scriptingcontrol flow
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.