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.
FYI, I added these two functions to my
.bash_profile
based on the answers found on SO. As Chris Johnson said, all answers using${x%/}
remove only one slash, these functions will do what they say, hope this is useful.The accepted answer will trim ONE trailing slash.
One way to trim multiple trailing slashes is like this:
Which outputs:
realpath
resolves given path. Among other things it also removes trailing slashes. Use-s
to prevent following simlinksYou can use the
${parameter%word}
expansion that is detailed here. Here is a simple test script that demonstrates the behavior:This works for me:
${VAR%%+(/)}
As described here http://wiki.bash-hackers.org/syntax/pattern
May need to set the shell option extglob. I can't see it enabled for me but it still works
In zsh you can use the
:a
modifier.This acts like
realpath
but doesn't fail with missing files/directories as argument.