Shell Conception

A shell is a command-line interface that allows users to interact with the operating system. It provides a way to execute commands, run scripts, and manage files and processes. The shell acts as an intermediary between the user and the operating system, interpreting user input and executing the corresponding commands.

1
2
3
4
5
6
7
8
cat /etc/shells
# List of valid login shells:
/bin/sh
/bin/bash
/bin/rbash
/bin/dash
/bin/zsh
/bin/ksh

example:

1
2
3
4
# Check the current shell
echo $SHELL
# Change the default shell to zsh
chsh -s /bin/zsh

1
2
3
#! /bin/bash
# This is a simple shell script that prints "Hello, World!" to the console.
echo "Hello, World!"

Shell Parameters

Shell parameters are variables that hold values and can be used in shell scripts or command-line operations. They can be categorized into different types, such as positional parameters, special parameters, and environment variables.

  • Positional Parameters: These are parameters that are passed to a shell script or command in a specific order. They are accessed using the $1, $2, etc. syntax, where $1 refers to the first parameter, $2 to the second, and so on.
  • Special Parameters: These are predefined parameters that have special meanings in the shell. For example, $# represents the number of positional parameters, $@ represents all the positional parameters as separate words, and $? represents the exit status of the last command executed.
  • Environment Variables: These are variables that are set in the shell environment and can be accessed by any command or script. They are typically defined using the export command and can be accessed using the $VARIABLE_NAME syntax.

Example:

1
2
3
4
5
6
#!/bin/bash
# This script demonstrates the use of shell parameters
echo "Number of parameters: $#"
echo "All parameters: $@"
echo "First parameter: $1"
echo "Second parameter: $2"

Shell Variables

Shell variables are used to store data that can be used and manipulated within a shell script or command-line session. They can hold various types of data, such as strings, numbers, or even the output of commands. Shell variables can be defined and accessed using the VAR_NAME=value syntax for assignment and $VAR_NAME syntax for access.
Example:

1
2
3
4
5
6
#!/bin/bash
# This script demonstrates the use of shell variables
NAME="Alice"
AGE=30
echo "Name: $NAME"
echo "Age: $AGE"

Shell Functions

Shell functions are reusable blocks of code that can be defined and called within a shell script or command-line session. They allow you to organize your code, improve readability, and avoid repetition. Shell functions are defined using the function keyword or simply by using the () syntax, and they can accept parameters and return values.
Example:

1
2
3
4
5
6
#!/bin/bash
# This script demonstrates the use of shell functions
greet() {
echo "Hello, $1!"
}
greet "Alice"

Shell Commands

Shell commands are instructions that can be executed in a shell environment. They can be built-in commands provided by the shell itself, or they can be external commands that are available in the system. Shell commands can be used to perform various tasks, such as file manipulation, process management, and system administration. Some common shell commands include ls for listing files, cd for changing directories, and echo for printing output to the console.
Example:

1
2
3
4
5
#!/bin/bash
# This script demonstrates the use of shell commands
echo "Current directory: $(pwd)"
echo "Files in the current directory:"
ls -l

Read more about shell commands in the Shell Commands section of the Bash manual.

Example Shell Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash

# ====== Demo: read user input ======
echo "Enter your full name:"
read fullname

echo "Enter your age:"
read age

echo "Hello $fullname, you are $age years old."
echo "----------------------------------------"

# ====== Demo: create sample file ======
echo "Creating sample file: users.txt"

cat <<EOF > users.txt
Alice,25,Developer
Bob,30,Designer
Charlie,28,Tester
EOF

echo "File content:"
cat users.txt
echo "----------------------------------------"

# ====== Demo: cut command ======
echo "Using cut to extract names (1st column):"
cut -d',' -f1 users.txt

echo "Using cut to extract ages (2nd column):"
cut -d',' -f2 users.txt
echo "----------------------------------------"

# ====== Demo: awk command ======
echo "Using awk to print formatted output:"
awk -F',' '{print "Name: "$1 ", Age: "$2 ", Job: "$3}' users.txt

echo "Using awk to filter (age > 27):"
awk -F',' '$2 > 27 {print $1 " is older than 27"}' users.txt
echo "----------------------------------------"

# ====== Demo: loop with read ======
echo "Reading file line by line:"
while IFS=',' read name age job
do
echo "$name works as a $job"
done < users.txt

echo "----------------------------------------"

# ====== Demo: simple condition ======
if [ "$age" -ge 18 ]; then
echo "You are an adult."
else
echo "You are underage."
fi