Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. [Setup] Bash Environment

[Setup] Bash Environment

Bash comes pre-installed on most macOS and Linux systems. You can open a terminal and start using it immediately — no special installation required.

For macOS / Linux

Opening a terminal gives you immediate access to Bash (or another shell such as zsh). You can check the version with the following command.

bash --version

Since macOS Catalina, the default shell has changed to zsh, but Bash is still installed and ready to use.

If you want the latest version of Bash, you can install it via Homebrew.

brew install bash

For Windows

There are several ways to use Bash on Windows.

MethodDescription
WSL (Windows Subsystem for Linux)Runs a Linux environment inside Windows. Provides a full-featured Bash. This is the recommended method.
Git BashA Bash environment bundled with Git for Windows. Supports basic commands.

Installing WSL

Open PowerShell as Administrator and run the following.

wsl --install

After restarting, the setup for a Linux distribution (Ubuntu by default) will begin. Set a username and password to complete the setup.

Creating and Running Scripts

1. Create the script file

Use a text editor to create a file named hello.sh with the following content.

#!/bin/bash

echo "Hello, World!"
echo "The Bash environment is set up successfully."

The first line, #!/bin/bash, is called a "shebang" line. It tells the OS to run this script with Bash.

2. Grant execute permission and run

chmod +x hello.sh
./hello.sh

chmod +x grants execute permission to the file, allowing you to run it directly with ./filename.

If Hello, World! and The Bash environment is set up successfully. are displayed, you're all set.

Running directly with the bash command

You can also run a script without granting execute permission by using the bash command directly.

bash hello.sh

About the Shebang Line

The shebang line is a special notation written on the first line of a script file, starting with #!.

NotationDescription
#!/bin/bashRuns with Bash. The most common form.
#!/usr/bin/env bashLooks up Bash via the PATH environment variable. More portable.
#!/bin/shRuns with a POSIX-compatible shell. Bash-specific features are not available.

Make it a habit to include a shebang line at the top of every script you write.

If the Command Is Not Found

Seeing bash: command not found in a terminal is rare, but PATH issues can occur after installing the latest version via Homebrew. Follow the steps below to check and fix the issue.

1. Check which Bash versions are installed

The system's built-in Bash.

/bin/bash --version

Bash installed via Homebrew.

/usr/local/bin/bash --version
/opt/homebrew/bin/bash --version

About the default shell on macOS

Since macOS Catalina (10.15), the default shell changed from Bash to zsh. When you open a terminal, it is zsh that is running, not Bash.

You can check your current shell with the following command.

Check the current shell.

echo $SHELL

Bash scripts can be run with bash scriptname.sh, so leaving the default shell as zsh is perfectly fine. If you want to change the default shell to Bash, run the following.

Change the default shell to Bash.

chsh -s /bin/bash

Setting the Homebrew version of Bash as the default

To use the latest version of Bash installed via Homebrew as your default shell, you first need to add it to the list of allowed shells.

Add it to the list of allowed shells (for Apple Silicon Macs).

echo '/opt/homebrew/bin/bash' | sudo tee -a /etc/shells

Change the default shell.

chsh -s /opt/homebrew/bin/bash

The Bash that comes with macOS is version 3 (due to GPLv2 license restrictions), while the Homebrew version is version 5, which supports newer features such as associative arrays and regex matching.

If you find any errors or copyright issues, please .