Linux Shell Script: Variable Demonstration (WECM100/b004.sh)

This page describes a basic Linux shell script (WECM100/b004.sh) designed to demonstrate variable usage for embedded beginners. It was executed within a Windows Subsystem for Linux (WSL) environment on Windows 11.

Environment:

System Information:

//$ uname -a:
Linux asus-oda 5.15.167.4-microsoft-standard-WSL2 
1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
//$ pwd >> /home/miura
//$ whoami >> miura

Script Execution:

$ cd ./WECM100
$ nano b004.sh  # Edit shell script
$ chmod +x b004.sh # Make script executable
$ ./b004.sh      # Execute script

Shell Script (WECM100/b004.sh):

#!/bin/bash
x=10
y=20

# Join characters ------------------
joi=$x$y
echo "{\"x\":$x,\"y\":$y,\"joi\":$joi}"

# Add integers ------------------
add=$((x+y))
echo "{\"x\":$x,\"y\":$y,\"add\":$add}"

Expected Output:

{"x":10,"y":20,"joi":1020}
{"x":10,"y":20,"add":30}

Explanation:

This script demonstrates two fundamental operations with variables in a Bash shell script:

  1. String Concatenation: The variables x and y are concatenated (joined) to form the string 1020 and stored in the joi variable.
  2. Integer Addition: The variables x and y are added using the arithmetic expansion $((x+y)), resulting in 30, which is stored in the add variable.

The script then outputs the values of x, y, joi, and add in JSON-like format for easy parsing or display.

This script is a simple introduction to variable manipulation in Bash, which is crucial for automating tasks and creating more complex scripts.