Site icon Leonid Mamchenkov

Handling arguments with spaces in bash

Way to often I get it wrong, so I decided to right this down…

When processing the list of arguments in your bash script, remember that often arguments such as file names contain spaces. The wrong way to go about this is:

#!/bin/bash
for FILE in $*
do
  echo "$FILE"
done

The right way to do it is:

#!/bin/bash
for FILE in "$@"
do
  echo "$FILE"
done

Advanced Bash-Scripting Guide explains the difference in “Internal Variables” chapter.

Exit mobile version