Basic Shell Script Variables (WECM100/b006.sh)

This page describes a basic shell script designed for embedded beginners, demonstrating the use of variables in a Linux shell environment. The script is executed within a Windows Subsystem for Linux (WSL) environment on Windows 11.

Environment

Script (b006.sh)

#!/bin/bash x=100 y="2.5" z="0.5" name="pizero" price=3200 # Add ------------------ sum=$((10+10+10+10+10+10+10+10+10+10+10+10+10+10+10+10+10+10+10+10)) echo "{\"x\":$x,\"y\":$y,\"add\":$sum}" # Increment x -------------------------- x=$((x + 100)) echo "{\"x\":$x}" # Create Sentence ------------------ echo "$name is $price yen" # Round ---------------------------- add=$(echo "$y + $z" | bc) round=${add%.*} echo "{\"y\":$y,\"round\":$round}" y="2.4" add=$(echo "$y + $z" | bc) round=${add%.*} echo "{\"y\":$y,\"round\":$round}"

Script Execution Steps

  1. Navigate to the script directory: cd ./WECM100
  2. Edit the script: nano b006.sh
  3. Make the script executable: chmod +x b006.sh
  4. Execute the script: ./b006.sh

Output

{"x":100,"y":2.5,"add":200} {"x":200} pizero is 3200 yen {"y":2.5,"round":3} {"y":2.4,"round":2}

Explanation

This script provides a simple introduction to shell scripting variables, arithmetic, and string manipulation, making it ideal for beginners working with embedded systems.