Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Dash: Shell Scripting Language

Introduction to Dash

Dash is a POSIX-compliant shell that aims to be lightweight and fast. It is commonly used in Unix-like operating systems for scripting tasks.

Basic Syntax and Usage

Dash syntax is designed to be compatible with POSIX standards. Here’s a simple example of a Dash script:


#!/bin/sh

# This is a comment
echo "Hello, World!"
                

In this example:

  • #!/bin/sh specifies the interpreter (Dash) for the script.
  • # This is a comment is a comment line.
  • echo "Hello, World!" prints "Hello, World!" to the standard output.

Variables and Data Types

Dash supports variables and basic data types:


# Variable declaration and assignment
NAME="Alice"
AGE=25

# Print variables
echo "Name: $NAME"
echo "Age: $AGE"
                

Variables in Dash are loosely typed and can hold strings and integers.

Control Structures

Dash includes standard control structures for conditional execution and looping:


# Conditional statement
if [ $AGE -ge 18 ]; then
    echo "Adult"
else
    echo "Minor"
fi

# Looping example
for i in $(seq 1 5); do
    echo "Count: $i"
done
                

Dash uses POSIX-compatible syntax for conditions and loops.

Functions

Dash supports functions for modular code organization:


# Function definition
say_hello() {
    name="$1"
    echo "Hello, $name!"
}

# Function call
say_hello "Bob"
                

Functions in Dash are defined using the standard POSIX syntax.

Conclusion

Dash is a lightweight and fast shell scripting language that adheres to POSIX standards. Its simplicity and compatibility make it suitable for scripting tasks in Unix-like operating systems.