在 bash 中修剪尾部和前导斜杠 - 加入参数替换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24212064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:40:50  来源:igfitidea点击:

Trim trailing and leading slash in bash - joining param substitutions?

bashshellsh

提问by whitfin

I'm trying to trim a possible /from the start and end of a string in bash.

我正在尝试/从 bash 中的字符串的开头和结尾修剪一个可能的值。

I can accomplish this via the following:

我可以通过以下方式完成此操作:

string="/this is my string/"
string=${string%/}
string=${string#/}
echo $string # "this is my string"

however, I would like to know if there's a way to join those two lines (2 + 3) to replace both at once. Is there a way to join the substitution, or is that the best I'm going to get?

但是,我想知道是否有办法加入这两行 (2 + 3) 以同时替换这两行。有没有办法加入替换,或者这是我将要得到的最好的方法?

Thanks in advance.

提前致谢。

采纳答案by konsolebox

Unfortunately there's no way to do that. However if you're sure that your string begins in / and ends in / you can trim it by ${P:M:N}format:

不幸的是,没有办法做到这一点。但是,如果您确定字符串以 / 开头并以 / 结尾,则可以按${P:M:N}格式修剪它:

string='/this is my string/'
string=${string:1:(-1)}

Adding a check could also help but it's still two statements:

添加检查也可能有所帮助,但它仍然是两个语句:

[[ $string == /*/ ]] && string=${string:1:(-1)}

Note: Solution is only available starting Bash 4.2.

注意:解决方案仅从 Bash 4.2 开始可用。

回答by Stormcloud

Comming to this very late... You can use bash variable substitution can remove a leading OR trailing optional slash, but it can't do both at the same time. If we execute:

来到这个很晚......您可以使用bash变量替换可以删除前导或尾随可选斜杠,但它不能同时进行。如果我们执行:

VAR1=/one/two/
VAR2=one/two
echo ${VAR1} ${VAR2}
echo ${VAR1#/} ${VAR2#/}
echo ${VAR1%/} ${VAR%/}

then we get:

然后我们得到:

/one/two/ one/two            # No change
one/two/ one/two             # No leading slashes
/one/two /one/two            # No trailing slashes

As we can see slashes inside the variables remain unaltered

正如我们所看到的,变量内的斜线保持不变

You can combine them using an intermediate variable as:

您可以使用中间变量将它们组合为:

VAR3=${VAR1#/}               # Remove optional leading slash
VAR3=${VAR3%/}               # Remove optional trailing slash
echo ${VAR3}

回答by Pankrates

If you are willing to use sedyou could do:

如果你愿意使用,sed你可以这样做:

string="/this is my string/"
echo $string | sed 's/^\/\(.*\)\/$//g'

This assumes the slashes are at the beginning and/or end of the string

这假设斜杠位于字符串的开头和/或结尾

回答by iruvar

If your /s are always at the start and end

如果你的/s 总是在开始和结束

echo "${string//\/}"
this is my string

If not

如果不

string="/this is /my string/"
IFS=/ read -ra x <<<"$string"
(IFS=/; printf '%s\n' "${x[*]:1:${#x[*]}-1}")
this is /my string

Or

或者

echo "$(IFS=/; set -- $string; printf '%s\n' "${*:2:$#-1}")"
this is /my string

回答by mklement0

Another pure bashsolution (v3.2 or above), using =~for regex matching and the special $BASH_REMATCHarray variable to reference capture group results.

另一个纯bash解决方案(v3.2 或更高版本),=~用于正则表达式匹配和特殊$BASH_REMATCH数组变量来引用捕获组结果。

string='/this is my string/'
[[ $string =~ ^/(.*)/$ ]] && string=${BASH_REMATCH[1]}

$stringis left untouched if its value is not enclosed in /.

$string如果其值未包含在 中,则保持不变/

回答by rishi

#!/bin/bash
truncate_leadingSlash()
{
 local string=""
 local len=${#string}
 while [[ "${string:$(expr $len - 1)}" == "/" ]]
 do
  string=${string:0:$(expr $len - 1)}
  len=${#string}
 done
 echo $string
}

truncate_leadingSlash "Autobiography/Of/Yogi//////"

Output: "Autobiography/Of/Yogi"

输出:“自传/Of/Yogi”