Company Logo

Bisht Bytes

Shell Programming Basics With Examples

Published On: 16 Aug 2024
Reading Time: 11 minutes

Overview


Shell programming is a way to automate tasks and manipulate the system using command-line scripts. Here's a quick overview of the basics with examples:

1. Declaring Variables

# Declaring a variable
name="John Doe"

Variables in shell scripting do not require a type declaration. There should be no spaces around the = sign.

2. Accessing Variables

echo "Hello, $name"

Use $ before the variable name to access its value.

3. String Operations

# Concatenation
greeting="Hello"
message="$greeting, $name!"

# Length of a string
length=${#message}

# Substring
substring=${message:0:5}

echo $message  # Output: Hello, John Doe!
echo $length   # Output: 16
echo $substring # Output: Hello

4. Numbers and Arithmetic Operations

# Declare and perform arithmetic
num1=5
num2=10

# Addition
sum=$((num1 + num2))

# Subtraction
difference=$((num2 - num1))

# Multiplication
product=$((num1 * num2))

# Division
quotient=$((num2 / num1))

echo "Sum: $sum"           # Output: 15
echo "Difference: $difference" # Output: 5
echo "Product: $product"       # Output: 50
echo "Quotient: $quotient"     # Output: 2

5. Conditional Statements

# If statement
if [ $num1 -gt $num2 ]; then
  echo "$num1 is greater than $num2"
else
  echo "$num1 is not greater than $num2"
fi

# Comparing strings
if [ "$name" == "John Doe" ]; then
  echo "Name is John Doe"
fi

6. Loops

# For loop
for i in 1 2 3 4 5; do
  echo "Number: $i"
done

# While loop
counter=1
while [ $counter -le 5 ]; do
  echo "Counter: $counter"
  counter=$((counter + 1))
done

7. Functions

# Define a function
greet() {
  echo "Hello, $1"
}

# Call the function
greet "Alice"

In the function above, $1 represents the first argument passed to the function.

8. Case Statements

# Case statement
case "$name" in
  "John Doe")
    echo "Hello, John!"
    ;;
  "Alice")
    echo "Hello, Alice!"
    ;;
  *)
    echo "Hello, stranger!"
    ;;
esac

9. Arrays

# Declare an array
fruits=("Apple" "Banana" "Cherry")

# Accessing array elements
echo ${fruits[0]}  # Output: Apple

# Loop through an array
for fruit in "${fruits[@]}"; do
  echo "Fruit: $fruit"
done

10. Reading User Input

# Read user input
echo "Enter your name:"
read user_name
echo "Hello, $user_name!"

The read command is used to capture user input.

11. Command Substitution

# Store the output of a command in a variable
current_date=$(date)
echo "Current Date and Time: $current_date"

Command substitution allows you to execute a command and store its output in a variable.

12. File Operations

# Creating a file
echo "This is a test file" > test.txt

# Appending to a file
echo "Appending some text" >> test.txt

# Reading a file
while IFS= read -r line; do
  echo "Line: $line"
done < test.txt

# Check if file exists
if [ -f "test.txt" ]; then
  echo "File exists"
else
  echo "File does not exist"
fi

File operations involve creating, appending, reading, and checking for file existence.

13. Functions with Return Values

# Function with a return value
add_numbers() {
  local sum=$(( $1 + $2 ))
  return $sum
}

add_numbers 3 7
result=$?
echo "Sum: $result"  # Output: Sum: 10

In shell scripting, you can return an integer value from a function using return, and capture it using $?.

14. Using Exit Status

# Check the exit status of the last command
mkdir new_dir
if [ $? -eq 0 ]; then
  echo "Directory created successfully"
else
  echo "Failed to create directory"
fi

The exit status of the last command executed can be checked using $?. A status of 0 indicates success, while non-zero indicates failure.

15. Here Documents

# Using a here document to create multi-line input
cat <<EOF > output.txt
This is the first line.
This is the second line.
EOF

Here documents are used to create multi-line strings and redirect them into a command or file.

16. Pipes and Redirection

# Using a pipe to pass the output of one command as input to another
ls -l | grep "test"

# Redirecting output to a file
ls -l > output.txt

# Redirecting error to a file
ls -l non_existent_dir 2> error.txt

# Redirecting both output and error to a file
ls -l non_existent_dir &> output_and_error.txt

Pipes (|) are used to send the output of one command as input to another. Redirection operators (>, 2>, &>) manage where the output and errors go.

17. Background Jobs

# Run a command in the background
sleep 5 &
echo "This will be printed immediately"

# Bring a background job to the foreground
fg %1

The & symbol runs a command in the background. fg brings a job back to the foreground.

18. Environment Variables

# Print an environment variable
echo "Home Directory: $HOME"

# Exporting a new environment variable
export MY_VAR="some value"

# Using an environment variable in a script
echo "My Var: $MY_VAR"

Environment variables are system-wide variables available to all processes. You can create and export your own environment variables as well.

19. Traps and Signal Handling

# Trap a signal (e.g., SIGINT)
trap "echo 'Ctrl-C was pressed. Exiting...'; exit" SIGINT

# Infinite loop to demonstrate trapping
while true; do
  echo "Press Ctrl-C to exit..."
  sleep 1
done

The trap command intercepts and handles system signals (like Ctrl-C). This is useful for cleaning up before a script exits.

20. Case Conversion

# Convert to uppercase
text="hello"
uppercase_text=$(echo $text | tr '[:lower:]' '[:upper:]')
echo "Uppercase: $uppercase_text"  # Output: HELLO

# Convert to lowercase
text="HELLO"
lowercase_text=$(echo $text | tr '[:upper:]' '[:lower:]')
echo "Lowercase: $lowercase_text"  # Output: hello

The tr command is used for translating or deleting characters from stdin.

21. Arrays with Indexes

# Declare an indexed array
my_array=(one two three four)

# Access elements by index
echo "First element: ${my_array[0]}"

# Get all elements
echo "All elements: ${my_array[@]}"

# Get the length of the array
echo "Array length: ${#my_array[@]}"

Indexed arrays store multiple values, accessed via their index.

22. Associative Arrays (Bash 4+)

# Declare an associative array
declare -A my_dict

# Add key-value pairs
my_dict=([name]="John" [age]=30 [city]="New York")

# Access elements by key
echo "Name: ${my_dict[name]}"

# Loop through keys
for key in "${!my_dict[@]}"; do
  echo "$key: ${my_dict[$key]}"
done

Associative arrays allow you to use strings as keys, providing more flexibility.

23. Date and Time

# Get the current date and time
now=$(date +"%Y-%m-%d %H:%M:%S")
echo "Current date and time: $now"

# Add days to the current date
future_date=$(date -d "+5 days" +"%Y-%m-%d")
echo "Date after 5 days: $future_date"

The date command is used to work with dates and times, allowing for formatting and date arithmetic.

24. String Manipulation with sed

# Replace text in a string
original="Hello World"
modified=$(echo $original | sed 's/World/Shell/')
echo "Modified: $modified"  # Output: Hello Shell

# Delete lines containing a specific word in a file
sed '/pattern/d' input.txt > output.txt

sed is a powerful stream editor for filtering and transforming text.

25. Working with JSON (jq)

# Extract value from JSON
json='{"name": "John", "age": 30}'
name=$(echo $json | jq -r '.name')
echo "Name: $name"  # Output: John

# Parsing and modifying JSON
age=$(echo $json | jq -r '.age')
new_age=$(($age + 1))
json=$(echo $json | jq --argjson new_age $new_age '.age = $new_age')
echo "Modified JSON: $json"

jq is a lightweight and flexible command-line JSON processor.

These additional topics will give you more advanced tools and techniques to work with in your shell scripts, making them more powerful and flexible.


Page Views: -