Free Shipping on All Orders! ๐Ÿšš We stand with Ukraine (5% of profits are donated) ๐Ÿ™

Dash

๐Ÿง˜โ€โ™€๏ธ Flow State and Fast Shells: Understanding Dash for Beginners

In the yoga world, thereโ€™s this concept called โ€œdrishtiโ€โ€”a focused gaze. Itโ€™s not just about where you look during a pose, itโ€™s about narrowing your attention to what truly matters, shedding the unnecessary to move with clarity and purpose. Think of a seasoned yogi in a crowded class: the room could be full of distractionsโ€”mirrors, other people wobbling in Tree Poseโ€”but they stay laser-focused, breath aligned, body balanced, movement efficient.

This is Dash. In the chaotic world of scripting and system processes, Dash is that yogi. Minimal. Fast. Focused. It doesnโ€™t care about fluffโ€”itโ€™s built to do one thing: run shell scripts quickly and efficiently.


๐Ÿ–ฅ๏ธ Soโ€ฆ What is Dash, Technically?

Dash stands for Debian Almquist Shell. Itโ€™s a lightweight, POSIX-compliant shell used primarily for scripting, not for interactive shell sessions like you might do in your terminal.

If youโ€™ve ever written a script and started it with:

#!/bin/sh

There's a good chance itโ€™s actually being run by Dash on many Linux systems (especially Debian-based ones like Ubuntu). Thatโ€™s because /bin/sh on those systems often points to Dashโ€”not Bash.

Why? Because Dash is fast. It's streamlined to execute shell scripts more quickly than its bulkier siblings, which matters a lot when your system is booting or running hundreds of tiny scripts behind the scenes.


๐Ÿ†š Dash vs Bash vs SH: Whatโ€™s the Difference?

Letโ€™s roll out the mat and get into the differences in style and purpose, yogi-style:

๐Ÿง˜ Dash: The Minimalist Yogi

๐Ÿฅ‹ Bash: The Vinyasa Flow Master

๐Ÿ‘ฃ SH: The Ancestral Guide


๐Ÿ’ก Why Should a Beginner Care About Dash?

Letโ€™s say youโ€™re just getting into scripting. You might not notice the milliseconds Dash saves at first. But imagine you're writing a script that runs every time your system boots, or every few minutes via cron. Multiply that over time, across hundreds of scriptsโ€”thatโ€™s impact.

Also, learning to write scripts that work in Dash (i.e., POSIX-compliant scripts) is a great habit. It forces you to keep things simple, portable, and robust. Itโ€™s like learning to hold your Warrior II pose with precision before trying to fly into a handstand.


๐Ÿง˜โ€โ™‚๏ธ Final Pose: Keep It Light

In yoga, the most advanced practitioners often return to the basicsโ€”mastering the breath, the stillness, the form. Likewise, in scripting, sometimes the most efficient solution is the simplest one. Dash embodies that philosophy.

So if youโ€™re just starting out with shell scripting, give Dash a try. Strip things back. Focus your gaze. Feel the breath of your system moving lighter and faster.

Namasteโ€”and happy scripting with the examples below. โœจ


Examples

Below is a top 100 list of common Dash shell scripting lines/blocks, tailored to beginners, but written with clarity and real-world relevance. These are all POSIX-compliant, meaning theyโ€™ll work in Dash (which doesn't support Bash-only features like arrays or syntax).


๐Ÿงพ Top 30 Common Lines of Code in Dash Shell Scripts


๐Ÿ”ง 1. Shebang Line (Start of Script)

#!/bin/dash

๐Ÿ“‚ 2. Defining a Variable

NAME="Alice"

๐Ÿ–จ๏ธ 3. Printing Text

echo "Hello, $NAME"

๐Ÿงช 4. Basic If Statement

if [ "$NAME" = "Alice" ]; then
  echo "Welcome, Alice!"
fi

๐Ÿ” 5. While Loop

i=1
while [ "$i" -le 5 ]; do
  echo "Loop $i"
  i=$((i + 1))
done

๐Ÿ”ƒ 6. For Loop

for FILE in *.txt; do
  echo "Found file: $FILE"
done

๐Ÿงญ 7. Check If File Exists

if [ -f "config.cfg" ]; then
  echo "Config file exists."
fi

๐Ÿ“ 8. Check If Directory Exists

if [ -d "/etc" ]; then
  echo "/etc exists."
fi

๐Ÿ†“ 9. Check If File is Executable

if [ -x "script.sh" ]; then
  echo "Script is executable."
fi

๐Ÿ–ฅ๏ธ 10. Running a Command

uptime

๐Ÿ’ฃ 11. Error Exit

echo "Something failed!" >&2
exit 1

๐Ÿ“œ 12. Reading User Input

echo "Enter your name:"
read USERNAME
echo "Hello, $USERNAME"

๐Ÿ“ฆ 13. Command Substitution

DATE=$(date)
echo "Today is $DATE"

๐Ÿ’ฌ 14. Quoting Variables Safely

echo "User input: "$USER_INPUT""

๐Ÿงน 15. Deleting a File

rm -f temp.txt

๐Ÿง 16. Function Definition

greet() {
  echo "Hello, $1"
}
greet "Dash"

โš–๏ธ 17. Else Statement

if [ "$USER" = "root" ]; then
  echo "Running as root"
else
  echo "Not root"
fi

๐Ÿ”„ 18. Elif Statement

if [ "$x" -gt 10 ]; then
  echo "Greater than 10"
elif [ "$x" -eq 10 ]; then
  echo "Exactly 10"
else
  echo "Less than 10"
fi

๐Ÿ“ถ 19. Checking Exit Status

some_command
if [ $? -eq 0 ]; then
  echo "Success"
else
  echo "Failed"
fi

๐Ÿ“ค 20. Redirect Output to File

echo "Log entry" >> logfile.txt

๐Ÿงพ 21. Read File Line by Line

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

โ›” 22. Test String Empty

if [ -z "$VAR" ]; then
  echo "VAR is empty"
fi

โœ… 23. Test String Not Empty

if [ -n "$VAR" ]; then
  echo "VAR is not empty"
fi

๐Ÿ•ณ๏ธ 24. Redirect stderr to File

command 2> errors.txt

๐Ÿ“› 25. Redirect stdout and stderr

command > output.txt 2>&1

๐Ÿงญ 26. Check Command Exists

if command -v curl >/dev/null 2>&1; then
  echo "curl is installed"
fi

๐Ÿ—œ๏ธ 27. String Comparison

if [ "$a" = "$b" ]; then
  echo "Equal"
fi

๐Ÿ“Š 28. Numeric Comparison

if [ "$a" -lt 100 ]; then
  echo "Less than 100"
fi

๐Ÿšฅ 29. Using Case Statement

case "$1" in
  start) echo "Starting...";;
  stop) echo "Stopping...";;
  *) echo "Usage: $0 {start|stop}";;
esac

๐Ÿš€ 30. Run Script with Arguments

echo "First arg: $1"
echo "All args: $@"

๐Ÿ”„ 31. Loop Through Arguments

for ARG in "$@"; do
  echo "Arg: $ARG"
done

๐Ÿ“‰ 32. Decrement a Number

i=5
i=$((i - 1))

๐Ÿงฎ 33. Basic Arithmetic

sum=$((3 + 4))
echo "Sum: $sum"

๐Ÿ”— 34. Combining Commands with &&

mkdir backup && echo "Directory created"

โ›“๏ธ 35. Combining Commands with ||

mkdir backup || echo "Failed to create directory"

๐Ÿงฉ 36. Checking Multiple Conditions

if [ "$x" -gt 5 ] && [ "$x" -lt 10 ]; then
  echo "x is between 6 and 9"
fi

๐Ÿ”ƒ 37. Infinite Loop

while :; do
  echo "Running forever"
  sleep 1
done

๐Ÿ“œ 38. Here Document (multi-line string)

cat <<EOF
Line 1
Line 2
EOF

๐Ÿšซ 39. Check If Command Fails

if ! grep "word" file.txt; then
  echo "Word not found"
fi

๐Ÿง  40. Use set -e to Exit on Error

set -e
# Any command failing here will exit the script

๐Ÿ”’ 41. Use set -u to Error on Unset Variables

set -u
echo "$UNSET_VAR"  # Will cause error if UNSET_VAR is undefined

๐ŸŒ 42. Ping a Host

ping -c 1 google.com >/dev/null 2>&1 && echo "Online"

โณ 43. Sleep for Seconds

sleep 5

๐Ÿงฑ 44. Check File is Non-Empty

if [ -s "file.txt" ]; then
  echo "File has content"
fi

๐Ÿƒ 45. Execute Script with Exec

exec ./child_script.sh

๐Ÿ“‚ 46. Get Script Directory

DIR=$(cd "$(dirname "$0")" && pwd)
echo "Script is in $DIR"

๐ŸŽฏ 47. Check If Directory Is Writable

if [ -w "/tmp" ]; then
  echo "Writable"
fi

๐Ÿ”ง 48. Check If Variable Is a Number

case $VAR in
  ''|*[!0-9]*) echo "Not a number" ;;
  *) echo "Itโ€™s a number" ;;
esac

๐Ÿงน 49. Remove All .log Files

rm -f *.log

๐Ÿ—“๏ธ 50. Get Todayโ€™s Date

TODAY=$(date +%F)
echo "Today is $TODAY"

๐Ÿ“ˆ 51. Compare Two Files

if cmp -s file1.txt file2.txt; then
  echo "Files are the same"
fi

๐Ÿ“– 52. Count Lines in File

LINES=$(wc -l < file.txt)
echo "Lines: $LINES"

๐Ÿ•ฐ๏ธ 53. Time a Command

START=$(date +%s)
your_command
END=$(date +%s)
echo "Took $((END - START)) seconds"

๐Ÿ”„ 54. Loop with Step (increment by 2)

i=0
while [ "$i" -le 10 ]; do
  echo "$i"
  i=$((i + 2))
done

๐Ÿ“ 55. Print Each Character of a String

STR="hello"
i=0
while [ "$i" -lt "${#STR}" ]; do
  echo "${STR:$i:1}"
  i=$((i + 1))
done

โš ๏ธ Not POSIX โ€” string slicing like ${STR:$i:1} isn't valid in Dash. A workaround would be needed. So this one should be skipped or rewritten using cut.


๐Ÿงช 55. (Fixed) Loop Over Each Char with POSIX fold

echo "$STR" | fold -w1 | while read -r ch; do
  echo "$ch"
done

๐Ÿท๏ธ 56. Truncate a File

> file.txt

๐Ÿ”š 57. End Script Early

exit 0

๐Ÿ’พ 58. Redirect to New File (Overwrite)

echo "New content" > file.txt

๐Ÿงฑ 59. Create Directory If Not Exists

[ -d "logs" ] || mkdir logs

๐Ÿ—ƒ๏ธ 60. Get Number of Arguments

echo "You passed $# arguments."

You got it. Here's the next 40 Dash-compatible scripting lines/blocks, taking us from 61 to 100, with practical, real-world use still in focus โ€” no Bash-only features, strictly POSIX/Dash-safe.


๐Ÿงพ Dash Shell Scripting: Snippets 61โ€“100


๐Ÿ“Š 61. Loop Over Numbers with seq

for i in $(seq 1 5); do
  echo "Number: $i"
done

๐Ÿ“Ž 62. Append to Variable

STR="hello"
STR="${STR} world"
echo "$STR"

๐Ÿงฉ 63. Default Value If Variable Is Empty

echo "${NAME:-Guest}"

๐Ÿ—‚๏ธ 64. Create Nested Directories

mkdir -p parent/child/grandchild

๐Ÿงผ 65. Trim Whitespace Using sed

TRIMMED=$(echo "$INPUT" | sed 's/^:space:*//;s/:space:*$//')

๐Ÿ“ 66. List All Files in Directory

for f in *; do
  echo "File: $f"
done

๐Ÿƒโ€โ™‚๏ธ 67. Run Command in Background

long_running_task &

โณ 68. Wait for Background Job

long_running_task &
PID=$!
wait "$PID"
echo "Done"

๐Ÿ›‘ 69. Trap Script Exit

trap 'echo "Exiting..."; cleanup' EXIT

๐Ÿชค 70. Trap Ctrl+C

trap 'echo "Interrupted!"; exit 1' INT

๐Ÿงพ 71. Check File Contains Text

if grep -q "needle" haystack.txt; then
  echo "Found it!"
fi

๐Ÿ—„๏ธ 72. Copy File

cp source.txt backup.txt

โœ‚๏ธ 73. Move (or Rename) File

mv file.txt archive.txt

๐Ÿ—‘๏ธ 74. Delete Empty Directories

find . -type d -empty -delete

๐Ÿงฎ 75. Round Down Division

echo $((7 / 2))  # Outputs 3

๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ 76. Get Current User

USER=$(whoami)

๐Ÿง  77. Check if Variable Is Set

if [ "${VAR+set}" = "set" ]; then
  echo "VAR is set"
fi

๐Ÿ•ณ๏ธ 78. Redirect Only Stdout (Not Stderr)

command > out.txt

๐Ÿ› ๏ธ 79. Create an Empty File

touch newfile.txt

๐Ÿ”— 80. Create a Symbolic Link

ln -s /path/to/original linkname

๐Ÿ” 81. Check If Port is Open (TCP)

if nc -z 127.0.0.1 80; then
  echo "Port open"
fi

๐Ÿงฌ 82. Replace Text in File (in-place)

sed -i 's/old/new/g' file.txt

โš ๏ธ Use sed -i '' on macOS (not POSIX, but still widely used).


๐Ÿ–ผ๏ธ 83. Get File Extension

EXT="${FILE##*.}"

๐Ÿงพ 84. Read Key Without Enter

stty -echo -icanon time 0 min 1
read -r key
stty sane

๐Ÿ“š 85. Print Script Usage

if [ $# -eq 0 ]; then
  echo "Usage: $0 <arg>"
  exit 1
fi

๐Ÿ” 86. Check If User Is Root

if [ "$(id -u)" -eq 0 ]; then
  echo "You are root"
fi

๐Ÿ“ฆ 87. Find Files by Name

find . -name "*.log"

๐Ÿงน 88. Delete Files Older Than 7 Days

find . -type f -mtime +7 -exec rm {} ;

๐Ÿ“Ž 89. Export a Variable to Subprocesses

export API_KEY="abc123"

๐Ÿ“‹ 90. Read First Line of File

read -r FIRST < file.txt

๐Ÿงฎ 91. Incrementing a Variable Inside a Loop

i=0
while [ "$i" -lt 3 ]; do
  echo "$i"
  i=$((i + 1))
done

๐Ÿ“ค 92. List Running Processes

ps -ef

๐Ÿ—ณ๏ธ 93. Choose Option With Case

case "$1" in
  yes) echo "You said yes" ;;
  no)  echo "You said no" ;;
  *)   echo "Unknown input" ;;
esac

๐Ÿ” 94. Check for Environment Variable

if [ -n "$HOME" ]; then
  echo "HOME is set"
fi

๐Ÿ“ฆ 95. Compress Files With tar

tar czf archive.tar.gz folder/

๐Ÿ“‚ 96. Unpack a .tar.gz

tar xzf archive.tar.gz

๐Ÿงช 97. Check Number of Args

if [ "$#" -ne 2 ]; then
  echo "Provide 2 arguments."
  exit 1
fi

๐Ÿ“ฅ 98. Download a File with wget

wget https://example.com/file.txt

๐Ÿ”Ž 99. Search Logs for Keyword

grep -i "error" logfile.txt

๐Ÿ“Ÿ 100. Get Script Name

SCRIPT_NAME=$(basename "$0")

๐Ÿ“˜ Bonus Tip: Always Test in a Dash Shell

Want to make sure your script is truly Dash-compatible?

Run it like this explicitly:

dash your_script.sh