Fundamentals 13 min read

Introduction to Linux Shell: Concepts, Script Types, Editing Standards, Execution Methods, and Variables

This article explains what a Linux/Unix shell is, lists common shell languages, outlines script editing conventions, describes various ways to execute shell scripts, and introduces environment, local, and special variables with practical command‑line examples.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Introduction to Linux Shell: Concepts, Script Types, Editing Standards, Execution Methods, and Variables
1. What is a shell

A shell is a command interpreter at the outermost layer of a Linux/Unix operating system, interacting directly with the user, interpreting input, executing commands, and returning output to the screen. It can run in interactive or non‑interactive mode.

[root@backup~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash

When commands are placed in a program file rather than typed on the command line, the file is called a shell script (similar to a Windows batch file).

2. Types of shell script languages

Common shells include Bourne‑style shells (sh, ksh, bash), C shell (csh), and others. On Red Hat and CentOS systems, bash is the most frequently used.

[root@backup~]# cat /etc/redhat-release
CentOS release6.5 (Final)

To view the default system shell:

[root@backup~]# echo $SHELL
/bin/bash

The bash version may differ between system releases:

[root@backup~]# bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
3. Shell script editing standards

Scripts are usually written in vi/vim and consist of Linux commands, bash commands, control‑flow statements, and comments.

Typical conventions:

First line specifies the interpreter, e.g., #!/bin/sh or #!/usr/bin/awk. On CentOS/RedHat the default /bin/sh links to bash.

Include author, version, and purpose information.

Add descriptive comments (preferably in English) to explain script functionality.

Use the .sh extension for script files.

Follow consistent spacing and indentation; write paired symbols ("", '', {}, [], ()) with surrounding spaces.

Write control‑flow constructs (if…then…fi, for…do…done) on separate lines and indent them for readability.

Example of a shebang line: #!/bin/sh Other possible shebangs:

#!/usr/bin/awk
#!/bin/sed
#!/usr/bin/expect
4. Shell script execution

When a script runs, the system first reads global environment variables, then the script’s own content.

Execution methods: bash script-name or sh script-name – used when the script lacks execute permission. ./script-name – requires execute permission ( chmod +x script-name). source script-name or . script-name – runs the script in the current shell, allowing variables to persist.

Examples:

[root@backup~]# cd /server/scripts/
[root@backupscripts]# sh baktable_auto.sh
[root@backupscripts]# ./congku.sh
-bash: ./congku.sh: Permission denied
[root@backupscripts]# . text.sh
root
5. Shell variables

Variables are divided into environment (global) variables and local (shell) variables.

Environment variables define the shell’s runtime environment (e.g., user name, PATH, HOME). They are usually set in ~/.bash_profile, /etc/profile, or /etc/profile.d/ and disappear when the user logs out unless persisted.

[root@backupscripts]# echo $HOME
/root
[root@backupscripts]# echo $USER
root

To create a custom environment variable:

NAME=beijing
export NAME
echo $NAME
beijing

To list all environment variables: env To unset a variable: unset USER Local variables exist only for the lifetime of the current shell session.

a=192.168.1.2-$a
echo "a=$a"
a=192.168.1.2-

Single quotes preserve literal text, double quotes allow variable expansion.

test=$(date)
echo $test
Wed Oct 12 23:09:23 EDT 2016

Special variables (important) $0 – script name (with path). $1 … $9 – positional parameters; use ${10} for the tenth. $# – number of positional parameters. $? – exit status of the last command (0 = success). $$ – PID of the current shell. $* – all positional parameters as a single string. $@ – all positional parameters as separate quoted strings.

Example of using $?:

[root@backupshell]# 123
-bash: 123: command not found
echo $?
127

Example of iterating with $* and $@:

set "I am" a man
for i in $*; do echo $i; done
I
am
a
man
for i in $@; do echo $i; done
I
am
a
man

For more details, consult the man bash manual.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Variables
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

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.