What is the simplest way to remove a trailing slash from each parameter?

sid_com picture sid_com · Jan 26, 2012 · Viewed 68.7k times · Source

What is the simplest way to remove a trailing slash from each parameter in the '$@' array, so that rsync copies the directories by name?

rsync -a --exclude='*~' "$@" "$dir"

The title has been changed for clarification. To understand the comments and answer about multiple trailing slashes see the edit history.

Answer

Sean Bright picture Sean Bright · Jan 26, 2012

You can use the ${parameter%word} expansion that is detailed here. Here is a simple test script that demonstrates the behavior:

#!/bin/bash

# Call this as:
#   ./test.sh one/ two/ three/ 
#
# Output:
#  one two three

echo ${@%/}