πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English | πŸ‡ͺπŸ‡Έ EspaΓ±ol | πŸ‡΅πŸ‡Ή PortuguΓͺs | πŸ‡ΉπŸ‡­ ΰΉ„ΰΈ—ΰΈ’ | πŸ‡¨πŸ‡³ δΈ­ζ–‡

What is Dash, Ubuntu's Default Shell? A Guide to Usage and Differences with Bash

If you use Ubuntu, you might have heard the term Dash (dash). Its existence becomes especially important when you're writing shell scripts. While the shell most users interact with daily is Bash, Dash is actually hard at work behind the scenes in Ubuntu's system. In this article, we'll break down the role of Dash as Ubuntu's default shell, its basic usage, and the differences with Bash that often cause confusion, all in a beginner-friendly way.

As a web creator, you'll increasingly encounter shell scripts for server configuration and deployment tasks. All the code in this article is designed to work by simply copying and pasting, so please get your hands dirty and experience the joy of making things "work"!


What is Dash, Anyway? And Why Does Ubuntu Use It?

Dash (Debian Almquist shell) is a command-line shell used in Unix-like operating systems. Its main feature is being lightweight and fast. Compared to the feature-rich Bash, Dash is limited to basic functions, which makes its script execution speed significantly faster.

Ubuntu (and its base, Debian) leverages this speed by adopting Dash as the default system shell (/bin/sh) for running system startup scripts and various background processes. This means that, separate from the interactive login shell (/bin/bash) we typically use in the terminal, Dash is efficiently handling tasks within the system. This division of laborβ€”"Dash for the system shell, Bash for the login shell"β€”is what supports Ubuntu's snappy performance.

Let's check what /bin/sh is linked to on your own system. Running the following command will show you what it really is.

ls -l /bin/sh

In most cases, you'll see /bin/sh -> dash, indicating that the actual entity of /bin/sh is Dash.


Basic Usage of Dash

Now, let's try running a simple script using Dash. The usage of Dash is very straightforward.

1. Create a Script File

First, create a simple script file with a text editor. We'll name it hello_dash.sh here.

nano hello_dash.sh

Once the editor opens, paste the following content and save it.

#!/bin/sh
# The line above is called a "shebang," and it specifies that this script should be run with /bin/sh (which is Dash).

echo "Hello, Dash!"

2. Grant Execute Permissions

Give the script file you created the permission to be executed.

chmod +x hello_dash.sh

3. Run the Script

Now that it's ready, let's run the script.

./hello_dash.sh

Success! "Hello, Dash!" should be displayed in your terminal. This is the most basic way to execute a Dash script. By specifying #!/bin/sh in the shebang, the script was interpreted and executed by Dash.


Advanced Example: Using Variables and Loops in Dash

For a more practical example, let's look at a script that uses variables and a while loop. The basic syntax is similar to Bash, but it's important to use POSIX-compliant syntax that is guaranteed to work in Dash.

The following script loops until a counter reaches 3, displaying the current count.

#!/bin/sh

# Define a variable
COUNT=1

# Continue the loop while COUNT is less than or equal to 3
while [ "$COUNT" -le 3 ]; do
  echo "Current count: $COUNT"
  # Add 1 to COUNT. Using the expr command is the most reliable way.
  COUNT=$(expr "$COUNT" + 1)
done

echo "Loop finished."

Save this script as something like loop_test.sh, grant it execute permissions, and run it with ./loop_test.sh. You should see the count from 1 to 3 displayed.


Points to Watch Out For: Incompatibilities Between Dash and Bash

The most common pitfall for beginners writing shell scripts is the functional difference between Dash and Bash. It's a frequent issue where a command that works perfectly fine in your usual terminal (Bash) results in an error in a script specified with /bin/sh (Dash).

In web development, scripts are often run as cron jobs on a server or executed by deployment tools, which means they are often run by the system's default shell, Dash. Therefore, unconsciously using features exclusive to Bash (known as "Bashisms") can cause errors that only appear in the production environment.

Below are some of the most common examples of incompatibility to be aware of.

1. Arrays Are Not Supported

In Bash, you can easily handle arrays like fruits=("Apple" "Banana" "Cherry"), but Dash does not support arrays.

Example that only works in Bash (Error in Dash):

#!/bin/bash
# Note the shebang is /bin/bash

fruits=("Apple" "Banana" "Cherry")
echo "The first fruit is ${fruits[0]}."

2. Brace Expansion {..} Is Not Available

In Bash, writing echo file{1..3}.txt expands to file1.txt file2.txt file3.txt, but Dash does not have this feature.

Example that only works in Bash (Displayed as-is in Dash):

#!/bin/bash

# This command generates three filenames in Bash
touch data_{a,b,c}.csv

To do the same thing in Dash, you would need to use a for loop or similar construct.


3. The `[[ ... ]]` Test Command Is Not Available

As mentioned earlier, Bash's extended test command [[ ... ]], while more powerful, cannot be used in Dash. You should stick to the POSIX-compliant [ ... ].

Example that only works in Bash (Error in Dash):

#!/bin/bash

NAME="hoge"
# [[ ... ]] can use && and || internally
if [[ "$NAME" == "hoge" && -n "$NAME" ]]; then
  echo "The name is hoge."
fi

Alternative code that also works in Dash:

#!/bin/sh

NAME="hoge"
# [ ... ] uses -a (AND)
if [ "$NAME" = "hoge" -a -n "$NAME" ]; then
  echo "The name is hoge."
fi

Note also that the string comparison uses = instead of ==.


Conclusion: Write Robust Scripts with Dash in Mind

In this article, we explained the role of Dash as Ubuntu's default shell, its basic usage, and its differences from Bash.

It might be confusing at first, but understanding these differences can prevent "it worked on my machine but not on the server" type of problems. Get accustomed to the world of Ubuntu dash and aim to create more reliable shell scripts!


Related Articles

If you want to learn more about advanced scripting and compatibility with Bash, this article is also recommended.