sea
May 5, 2015, 7:34pm
1
Heyas
I am trying to remove a tailing space, with substitution.
Already tried some multiple escapes with no luck, is that even possible?
echo $CHROOT
[ -z "$CHROOT" ] || \
CHROOT="${CHROOT/\/$}"
echo $CHROOT
return
And all i get is:
:) paths $ CHROOT=/usr/local/
+ paths $ . *
/usr/local/
/usr/local/
$SHELL -version
GNU bash, version 4.3.33(1)-release (x86_64-redhat-linux-gnu)
The 2nd printed output, would be expected to be:
/usr/local
Thank you
EDIT:
I can check if the last string is a slash or not, but i thought a subs would work just fine?
Try:
CHROOT=${CHROOT%/}
This strips a trailing slash. Your attempt to use substitution would work, but the pattern isn't regex. You'd use ${CHROOT/%\//}
See relevant section of the bash manual:
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with �/�, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with �#�, it must match at the beginning of the expanded value of parameter. If pattern begins with �%�, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is �@� or ��, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with �@� or � �, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.
1 Like
neutronscott solution is the % (Remove matching suffix pattern) expansion not replace. It happens to product the same result in this case. The replace version should be:
${CHROOT/%\//}
I'll try and highlight the difference here:
$ CHROOT=/test/this/
$ echo ${CHROOT/%\//}
/test/this
$ echo ${CHROOT%/}
/test/this
$ echo ${CHROOT/%\//Z}
/test/thisZ
$ echo ${CHROOT%/Z}
/test/this/
In the 3rd example will replace the last slash with Z
In the 4th example we try and delete /Z from end of string (no match, so nothing deleted).