What is Dash? Explaining the Features and Uses of this Lightweight, High-Speed Shell
When using Linux or macOS, you often hear terms like "shell" and "Bash." But have you ever heard of a shell called Dash? In fact, Dash is an unsung hero working hard behind the scenes, especially in Debian-based Linux distributions like Ubuntu.
This article is for web creators who have heard the name but don't know much about it. We'll explain what Dash is, its features, how it differs from Bash, and its practical uses with sample code you can run just by copying and pasting. By the end of this article, you'll understand the appeal of Dash and be able to use it effectively!
What is the Dash Shell?
Dash stands for "Debian Almquist shell," and as the name suggests, it's a shell developed by the Debian project. It was originally a port of the Almquist Shell (ash) used in NetBSD to Linux.
Its main feature is that it is lightweight and fast. "Bash," which we casually use in the terminal, is a "feature-rich, deluxe shell" with extensive command history completion and convenient proprietary functions. In contrast, Dash achieves surprising agility by limiting its features to the minimum set defined by POSIX (a standard for portable OS interfaces).
Particularly in Ubuntu and Debian, the system's default shell, /bin/sh, is actually a symbolic link (like a shortcut) to Dash. This allows system startup scripts to run quickly, contributing to the overall performance improvement of the OS.
Main Advantages (Features) of Dash
Let's take a closer look at what makes Dash so great.
- π Fast Execution Speed: Because its features are simple, program startup and script interpretation are extremely fast. The speed difference between it and Bash is especially noticeable in processes that loop thousands of times or when executing many small scripts.
- Low Memory Usage: Being lightweight, it helps to conserve memory. This is a significant advantage in embedded systems with limited memory or in server environments running numerous containers simultaneously.
- POSIX Compliance: Dash is very faithful to the POSIX standard. This means that a shell script written to run in Dash is highly likely to run correctly in other POSIX-compliant shells (like zsh on macOS). Dash serves as an excellent test environment when you want to improve the portability and compatibility of your scripts.
Let's Try It! Getting Hands-On with Dash
Seeing is believing. Let's get hands-on with Dash to experience its features for ourselves!
1. Check Your Current Shell
First, let's check what your current default shell is. Open your terminal and run the following command. In most cases, it will display "bash" or "zsh".
$ ps -p $$
PID TTY TIME CMD
5678 pts/0 00:00:00 bash
2. Start Dash
Now, let's start Dash. Just type `dash` on the command line and press Enter. When the prompt (the input symbol) changes to `$`, you're in the world of Dash.
$ dash
$
To return to your original shell (like Bash), just type `exit`.
3. [Experience] Speed Comparison with Bash
Let's compare how fast Dash is with a simple loop. Here, we'll run a script that simply counts from 1 to 100,000 in both Bash and Dash. You can use the `time` command to measure the processing time.
First, let's run it with Bash.
$ time bash -c 'i=0; while [ $i -lt 100000 ]; do i=$((i+1)); done'
real 0m1.523s
user 0m1.518s
sys 0m0.005s
*Execution time will vary depending on your environment.
Next, let's run the exact same process with Dash.
$ time dash -c 'i=0; while [ $i -lt 100000 ]; do i=$((i+1)); done'
real 0m0.384s
user 0m0.381s
sys 0m0.003s
What do you think? In my environment, the process that took about 1.5 seconds in Bash was completed in just under 0.4 seconds in Dash. That's nearly a 4x speed difference! As you can see, Dash's high speed is incredibly effective for repeatedly executing simple scripts.
Points to Note: Features in Bash but Not in Dash
While Dash is fast, it lacks some of the convenient features that are standard in Bash. This is called "Bashism." Be careful, because if you write a script using Bash syntax without realizing it, it will cause an error in Dash.
Example 1: Arrays Cannot Be Used
In Bash, you can easily define arrays using `()`, but Dash has no concept of arrays.
In Bash (works correctly)
$ bash -c 'fruits=("apple" "banana" "orange"); echo ${fruits[1]}'
banana
In Dash (results in an error)
$ dash -c 'fruits=("apple" "banana" "orange"); echo ${fruits[1]}'
dash: 1: Syntax error: "(" unexpected
Example 2: Extended Test Command `[[ ... ]]` Cannot Be Used
Bash's `[[ ... ]]` is highly functional, allowing for string matching and regular expressions, but it is not part of the POSIX standard. In Dash, you must use the traditional `[ ... ]`.
In Bash (works correctly)
$ bash -c '[[ "hello" == "hello" ]] && echo "Yes"'
Yes
In Dash (results in an error)
$ dash -c '[[ "hello" == "hello" ]] && echo "Yes"'
dash: 1: [[: not found
How to write it correctly in Dash
To do the same thing in Dash, use `[` and a single equals sign `=`.
$ dash -c '[ "hello" = "hello" ] && echo "Yes"'
Yes
Example 3: Brace Expansion `{...}` Cannot Be Used
Brace expansion, which is useful for generating sequential filenames, is also a Bash feature.
In Bash (works correctly)
$ bash -c 'echo file-{1..3}.txt'
file-1.txt file-2.txt file-3.txt
In Dash (it's displayed as is)
$ dash -c 'echo file-{1..3}.txt'
file-{1..3}.txt
Main Uses for Dash
So, considering these characteristics, in what situations is Dash used?
- System startup scripts: This is its most common use. It speeds up boot time by quickly processing the numerous scripts that run when the OS starts.
- Executing shell scripts: You can improve performance by writing scripts that require high processing speed (e.g., analyzing large log files, simple batch processing) in Dash.
- Resource-constrained environments: Dash is preferred in environments with limited memory and CPU power, such as IoT devices, embedded systems, and Docker containers.
- Script compatibility checks: A good way to check if your shell script depends on Bash-specific features (i.e., if it's POSIX-compliant) is to run it with `dash myscript.sh`.
On the other hand, Dash is not well-suited as an interactive shell (the shell we normally use in the terminal). Its command completion, history search, and aliasing features are poor compared to Bash, which would decrease your daily work efficiency.
Conclusion: Use Bash and Dash Wisely
In this article, we explained the lightweight and fast shell, "Dash."
- Dash is a shell that pursues lightness and speed by limiting its features. It excels at behind-the-scenes system tasks and fast script execution.
- Bash is a multi-functional shell packed with convenient features. It excels at interactive use, where we type commands daily.
It's important to understand the strengths and weaknesses of each shell. It's not recommended to blindly change your default login shell from Bash to Dash just because "it's faster."
By remembering Dash as an "unsung hero" that speeds up your system and as a "secret weapon" to make your shell scripts faster, you'll add another tool to your developer toolkit. Be sure to use Dash where it's most appropriate!