copied to clipboard!
string basename

[basename] Run and understand. basename command to get file name from folder name containing spaces

created-2025/11/13 updated-2026/03/15

Introduction

This repository explains how to execute the basename command to get the file name from a folder name that contains spaces in a shell script.

Commands that need to be installed

  1. git

Quickstart

If you have already installed the above command, run the following command to download it from the repository, then move to the directory and check the behavior of the basename command, which retrieves the file name from a folder name that contains spaces in a shell script.

ubuntu

git clone https://github.com/trygfmi/basename_space
cd basename_space
cat basename_space.sh
chmod 755 basename_space.sh
./basename_space.sh "./hello world/basename command/file_name.txt"
Output results
basename: extra operand ‘command/file_name.txt’
Try 'basename --help' for more information.
basename  $file
basename "$file" file_name.txt
basename '$file' $file

macos

MacPorts

source ~/bashrc_folder/macports_alias
git clone https://github.com/trygfmi/basename_space
cd basename_space
cat basename_space.sh
chmod 755 basename_space.sh
./basename_space.sh "./hello world/basename command/file_name.txt"
Output results
basename  $file  hello basename file_name.txt
basename "$file" file_name.txt
basename '$file' $file

windows

WSL2

git clone https://github.com/trygfmi/basename_space
cd basename_space
cat basename_space.sh
chmod 755 basename_space.sh
./basename_space.sh "./hello world/basename command/file_name.txt"
Output results
basename: extra operand ‘command/file_name.txt’
Try 'basename --help' for more information.
basename  $file
basename "$file" file_name.txt
basename '$file' $file

MSYS2 MINGW64

git clone https://github.com/trygfmi/basename_space
cd basename_space
cat basename_space.sh
chmod 755 basename_space.sh
./basename_space.sh "./hello world/basename command/file_name.txt"
Output results
basename: extra operand ‘command/file_name.txt’
Try 'basename --help' for more information.
basename  $file
basename "$file" file_name.txt
basename '$file' $file

Procedure

ubuntu

Click to open details

Advance confirmation

Enter the following command into the terminal and if command not found does not appear, it is OK.

git --version

preinstall

If the command not found appears in the terminal, please install it using the following command.

sudo apt install git

command

Executing the following command should output a string similar to the following:

git clone https://github.com/trygfmi/basename_space
cd basename_space
cat basename_space.sh
chmod 755 basename_space.sh
./basename_space.sh "./hello world/basename command/file_name.txt"
detail
basename: extra operand ‘command/file_name.txt’
Try 'basename --help' for more information.
basename  $file
basename "$file" file_name.txt
basename '$file' $file

macos

Click to open details

Advance confirmation

Enter the following command into the terminal and if command not found does not appear, it is OK.

source ~/bashrc_folder/macports_alias
git --version

※MacOS uses the MacPorts package manager to manage commands. If you haven’t installed it yet, please see the MacPorts installation instructions at the link below.

It also allows you to set aliases for commands so that you can call them without conflicting with existing environments.

If you are using this blog for the first time, please refer to the following two articles to set up your environment.

preinstall

If the command not found appears in the terminal, install it using the following command and set an alias.

sudo port install git
echo 'alias git="/opt/local/bin/git"' >> ~/bashrc_folder/macports_alias

COMMAND

Executing the following command should output a string similar to the following:

source ~/bashrc_folder/macports_alias
git clone https://github.com/trygfmi/basename_space
cd basename_space
cat basename_space.sh
chmod 755 basename_space.sh
./basename_space.sh "./hello world/basename command/file_name.txt"
detail
basename  $file  hello basename file_name.txt
basename "$file" file_name.txt
basename '$file' $file

windows

Click to open details

Advance confirmation

Enter the following command into the prompt and if command not found does not appear, it’s OK.

WSL2
git --version
MSYS2 MINGW64
git --version

※Windows has been confirmed to work with WSL2 and MSYS2 MINGW64. Please install WSL2 if possible, and if you are unable to set it up, you can run it by installing MSYS2, but there may be some commands that can only be run in WSL2. Please refer to the following article for instructions on how to install WSL2 and MSYS2.

preinstall

If the command not found appears in the prompt, please install it using the following command.

WSL2
sudo apt install git
MSYS2 MINGW64
pacman --sync git

command

Executing the following command should output a string similar to the following:

WSL2
git clone https://github.com/trygfmi/basename_space
cd basename_space
cat basename_space.sh
chmod 755 basename_space.sh
./basename_space.sh "./hello world/basename command/file_name.txt"
detail
basename: extra operand ‘command/file_name.txt’
Try 'basename --help' for more information.
basename  $file
basename "$file" file_name.txt
basename '$file' $file
MSYS2 MINGW64
git clone https://github.com/trygfmi/basename_space
cd basename_space
cat basename_space.sh
chmod 755 basename_space.sh
./basename_space.sh "./hello world/basename command/file_name.txt"
detail
basename: extra operand ‘command/file_name.txt’
Try 'basename --help' for more information.
basename  $file
basename "$file" file_name.txt
basename '$file' $file

Afterword

basename $file will cause an error if the variable name contains spaces, as in the output on Ubuntu and Windows. It also outputs the first character of a folder name containing spaces and the file name, as in the output on macOS. Therefore, it’s a good idea to enclose the variable name in double quotes, such as “basename “$file.”

Leave a Reply

Your email address will not be published. Required fields are marked *

©︎ 2025-2026 todo